Sending Mail Using SMTP in C# .NET

In .Net, sending mail via SMTP is quite simple because all the references that you need to send mail have been incorporated by the IDE and a simple function would look as below: /// <summary> /// /// </summary> /// List of mail recepients /// email subject /// mail content public void fnSMTPMail(List To, string Subject, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Force send receive action in C# Outlook Interop

//outlook functionality of send and receive .* enable send then disable * public void fnSendRecieve() { try { oApp = new OL.Application(); oNameSpace = oApp.GetNamespace("MAPI"); oNameSpace.Logon(null, null, false, false); oNameSpace.SendAndReceive(false); fnDisableSendRecieve(); } catch (Exception ex) { util.fnLogError(ex.Message, "Send/Recieve mail in Outlook class"); } } private void fnDisableSendRecieve() { try { oApp = new OL.Application(); oNameSpace […]
Continue reading…

Enjoyed this post? Share it!

 
 

Read mail from a public folder using Outlook Interop in C#

public void fnReadMail() { try { oApp = new OL.Application(); oNameSpace = oApp.GetNamespace("MAPI"); oNameSpace.Logon(null, null, false, false); OL.MAPIFolder oInboxFolder = oNameSpace.GetDefaultFolder(OL.OlDefaultFolders.olFolderInbox); OL.Items oItems = oInboxFolder.Items.Restrict("[Unread] = true"); foreach (object obj in oItems) { object[] args = new object[] { }; //navigating to the public folder in the outlook folders object retVal = obj.GetType().InvokeMember("Class", BindingFlags.Public | […]
Continue reading…

Enjoyed this post? Share it!

 
 

Catch SmtpException with InnerWebException

The following is an example on how to catch SmtpException with InnerWebException while sending email in ASP.NET C#. catch (SmtpException ex) { // Invalid hostnames result in a WebException InnerException that provides // a more descriptive error, so get the base exception Exception inner = ex.GetBaseException(); Console.WriteLine("Could not send message: " + inner.Message); }
Continue reading…

Enjoyed this post? Share it!

 
 

Sending EMail asynchronously in ASP.NET C#

To send email asynchronously, in csharp, we should create an event handler for SendCompleted event on the smtpclient object. The SmtpClient.SendAsync method needs to be invoked to send the message asynchronously. Example for sending email asynchronously: MailMessage m = new MailMessage ("item@w3mentor.com", "raja@w3mentor.com", "This is the subject for the authorized email.", "This is the body […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sending Mail using SSL in ASP.NET C#

To send email using secure sockets layer (SSL), in csharp, we should set the SmtpClient.EnableSsl propery to true. Example for sending email with SSL: MailMessage m = new MailMessage ("item@w3mentor.com", "raja@w3mentor.com", "This is the subject for the authorized email.", "This is the body of the authorized mail!…");   // Send the message using authorization SmtpClient […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sending Mail using SMTP authorization credentials in ASP.NET C#

We can explicitly provide a valid username and password or use the cached network credentials while sending email using the System.Net.Mail class. Example for sending email with SMTP authorization credentials: MailMessage m = new MailMessage ("item@w3mentor.com", "raja@w3mentor.com", "This is the subject for the authorized email.", "This is the body of the authorized mail!…");   // […]
Continue reading…

Enjoyed this post? Share it!

 
 

Types of exceptions in sending Mail in ASP.NET C#

1.Exception: InvalidOperationException Cause: Undefined server hostname 2.Exception: SmtpException with an inner WebException Cause: server hostname not found. 3.Exception: SmtpFailedRecipientException Cause: Recipient does not have a mailbox on the mail server. 4.Exception: SmtpException Cause: Transmission problems or invalid user trying to send email
Continue reading…

Enjoyed this post? Share it!

 
 

Send HTML E-mails with embedded image content using ASP.NET C#

To send emails with HTML formatting and to embed resources such as images, in csharp, we create an alternateview to hold the embedded resource link and then create a linked resource object for each embedded image. The content ID of the linked resource is used to embed the image in the html email. add the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Send HTML E-mails using ASP.NET C#

To send emails with HTML formatting, in csharp, we create a MailMessage and then add the HTML-tagged content to the Mail Body. The important thing to remember is to set the IsBodyHtml attribute to True on the mail object. Example for sending HTML Email: MailMessage m = new MailMessage(); m.From = new MailAddress("ir@w3mentor.com", "Raja Item"); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Use stream as a file attachment with mimetype in csharp

The example given below demonstrates how to use a Stream as a file attachment and how to specify the MIME type when sending email using c#. Example: MailMessage m = new MailMessage(); m.From = new MailAddress("ir@w3mentor.com", "Raja Item"); m.To.Add(new MailAddress("su@w3mentor.com", "Sekaran Uma")); m.Subject = "File attachment!"; m.Body = "See the attached file."; Stream sr = […]
Continue reading…

Enjoyed this post? Share it!

 
 

Send emails with attachments in ASP.NET C#

To send emails with attachments, in csharp, we can create a MailMessage and then add attachments by adding Attachment objects to MailMessage.Attachments AttachmentCollection by calling the MailMessage.Attachments.Add method. The mimetype can be optionally specified in the attachment. Example to send emails with attachments in ASP.NET C#: MailMessage m = new MailMessage(); m.From = new MailAddress("ir@w3mentor.com", […]
Continue reading…

Enjoyed this post? Share it!

 
 

Send email to multiple recipients in C#

To send emails to multiple people/recipients, we can create a MailMessage using a constructor with no parameters and then adding MailAddress objects to the MailMessage.To property. The MailMessage.To property is a MailAddressCollection type. We can separately specify MailMessage.From, MailMessage.Subject, and MailMessage.Body properties. MailMessage m = new MailMessage(); m.From = new MailAddress("ir@w3mentor.com", "Raja Item"); m.To.Add(new MailAddress("su@w3mentor.com", […]
Continue reading…

Enjoyed this post? Share it!

 
 

MailAddress object in C#

The MailAddress object in csharp allows us to specify the details of a single mail sender or recipient as an object. This object can be used as a parameter to the MailMessage object to send emails in csharp. We can create a single MailAddress object as MailAddress ma = new MailAddress("item@w3mentor.com", "Item Raja"); We can […]
Continue reading…

Enjoyed this post? Share it!