Thursday, February 12, 2015

Send Email with attachment - HTML attachment

Note:

While sending email with google SMTP server, we may get following error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required

In order to avoid such error -
Go to the link https://www.google.com/settings/security/lesssecureapps and turn on feature access for less secure app.


=========================================================================

Following code will give us an idea how to create an HTML attachment and send it via email.

Step 1:  Create a simple asp.net application and on it's design page add a button control. On the click event of this button control we have to write code to create a HTML file and sent it via email as an attachment.

Step 2: On Button click event, write following code.
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
//html attachment
//1. Format : Head, title , body
string testContent= create_HTML_FILE();
lblHTML.Text = testContent;
//send Email
sendEmail();
lblMail.Text = "check email";
}
catch (Exception)
{
//throw;
}
}

Here create_HTML_FILE(); and sendEmail(); are two user defined methods.


Step 3: create_HTML_FILE();

Now we will write a code to create a HTML file. We are saving this file in project root folder.
private string create_HTML_FILE()
{
string strHTML = "";
try
{
string strHStart = "<HTML>";
string strHEnd = "</HTML>";
string strTitle = "<Title>test title</Title>";
string strHeader = "<H1>This is test Header</H1>";
string strBody = "<Body>this is test body this is test body this is test body this is test body this is test body this is test body </Body>";
strHTML = strHStart + strTitle +strHeader + strBody + strHEnd;
FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory+"//testKB.html", FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write(strHTML);
sw.Flush();
sw.Close();
fs.Close();
}
catch (Exception)
{
//throw;
}
return strHTML;
}
We can format Body, Header, Title  as per our requirement.
Note that we are saving file in root directory of project AppDomain.CurrentDomain.BaseDirectory
and name of the file is testKB.html



Step 4: sendEmail();

Now Write code to send email with attachment
public void sendEmail()
{
try
{
string toEmail = "to email";
string FromEmail = "from email";
MailAddress maTO = new MailAddress(toEmail);
MailAddress maFROM = new MailAddress(FromEmail);
MailMessage mm = new MailMessage();
mm.From = maFROM;
mm.To.Add(maTO);
mm.Subject = "html attachment";
mm.Priority = MailPriority.Normal;
mm.Body = "find attachment";
Attachment attach = new Attachment(AppDomain.CurrentDomain.BaseDirectory + "\\testKB.html");
mm.Attachments.Add(attach);
SmtpClient sc = new SmtpClient("smtp.gmail.com", 587);
sc.UseDefaultCredentials = false;
sc.Credentials = new System.Net.NetworkCredential("email", "password");
sc.EnableSsl = true;
sc.DeliveryMethod = SmtpDeliveryMethod.Network;
sc.Send(mm);
}
catch (Exception ex)
{
lblMail.Text = ex.ToString();
// throw;
}
}
 
Execute the code and then check email with html attachment received in your inbox.


 

No comments:

Post a Comment