Using SMTP with IIS
You can set up both IIS and SMTP services via the Windows control panel. The SMTP service’s role is to accept and deliver the messages using the server’s configuration. It may deliver the messages directly, or utilise a smart host to deliver the message instead. When a smart host is enlisted, all messages are forwarded to it for delivery.
A little more information is appropriate for debugging. The SMTP service uses a directory structure to contain messages prior to delivery with the default directory being C:\Inetpub\mailroot. It contains numerous subdirectories including Queue, Drop, and Badmail. If you’re unable to configure your instance of the SMTP Service for delivery, you can find the message in an EML file in the Queue subdirectory. The Badmail directory contains messages that that couldn’t be delivered. Now, let’s take a look at sending mail messages from your code.
Sending e-mail messages
To compose an e-mail message in your code, you need to start by creating an instance of the MailMessage class, as shown in the following C# snippet:
MailMessage msg = new MailMessage();
Be sure to include the System.Web.Mail namespace in your code:
using System.Web.Mail;
Once the object is instantiated, the various properties of the MailMessage class are used per your application. The following lines set the recipient, sender, and subject of the message:
msg.To = “test@test.com”; msg.From = “me@test.com”; msg.Subject = “Test Message”;
The next step is setting our mail server via the SmtpServer object’s SmtpServer property:
SmtpMail.SmtpServer = “smtp server name or address”;
The final step is sending the message by passing our MailMessage object to the SmtpMail object’s Send method:
SmtpMail.Send(msg);
Here I am Providing you the Complete Example of how to send Email with Inline Image / Multiple Attachments …etc…
—————————————- Button Event Code —————————————-
protected void Button1_Click(object sender, EventArgs e) { string from = “”, to = “”, cc = “”, subject = “”, body = “”, attachedfiles = “”;
from = “yogesh@gmail.com”; to = “yrb.yogi@gmail.com;bhadauriya.yogesh@gmail.com”; // multiple entry are separated bt ‘;’ cc = “yrb.yogigmail.com”; subject = “Testing Email Module”;
//Read Mail Template StreamReader sr; StringBuilder sb = new StringBuilder(); string BodyOfMail = “”; sr = File.OpenText(Server.MapPath(”~/MailTemplate.txt”)); sb.Append(sr.ReadToEnd()); body = sb.ToString();
//attachments attachedfiles = “E:\\Images\\1.jpg;E:\\Images\\2.jpg;”; emailClass.SendEmail(from, to, cc, subject, body, attachedfiles); }
—————————————- MailTemplate.txt —————————————-
<title>Untitled Document</title> </head> <body> <form id=”form1″ name=”form1″ method=”post” action=”"> <table width=”100%” border=”0″ cellspacing=”10″ cellpadding=”10″> <tr> <td bgcolor=”#FFFFFF”> <br /> <table width=”550″ border=”0″ cellpadding=”2″ cellspacing=”0″ bordercolor=”#FFFFFF” id=”Comment Table2″> <tr> <td > Hello Friend Name,<br /> <br /> Your friend has been send you this offer. <br /> </td> </tr> <tr> <td align=”right”> <table width=”530″ border=”0″ cellpadding=”0″ cellspacing=”0″> <tr> <td > <img src=”{images/logo1.jpg” /> </td> <td > <img src=”{images/logo2.jpg” /> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> </form> </body> </html>—————————————- emailClass Class —————————————-
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net.Mail; using System.Net;
///
//to authenticate we set the username and password properites on the SmtpClient mailObj.Credentials = new NetworkCredential(”yogesh@gmail.com”, “password”);
mailMsg.From = new MailAddress(from);
string[] temp = to.Split(’;'); for (int i = 0; i <> 1) mailMsg.To.Add(temp[i].ToString()); }
temp = cc.Split(’;'); for (int i = 0; i <> 1) mailMsg.CC.Add(temp[i].ToString()); } temp = attachedFiles.Split(’;'); for (int i = 0; i <> 1) mailMsg.Attachments.Add(new Attachment(temp[i].ToString())); }
mailMsg.Subject = subject; mailMsg.Body = body; mailMsg.IsBodyHtml = true; int count = 0; string oldChar = ExtractImages(body, ref count); Random RGen = new Random(); while (oldChar != “”) { string imgPath = oldChar; int startIndex = imgPath.ToLower().IndexOf(”images/”); if (startIndex > 0) { imgPath = imgPath.Substring(startIndex); imgPath = imgPath.Replace(”/”, “\\”); System.Net.Mail.Attachment A = new Attachment(HttpContext.Current.Request.PhysicalApplicationPath + “\\” + imgPath); A.ContentId = RGen.Next(100000, 9999999).ToString(); body = body.Replace(oldChar, “cid:” + A.ContentId); mailMsg.Attachments.Add(A); oldChar = ExtractImages(body, ref count); } else { oldChar = ExtractImages(body, ref count); } } mailMsg.Body = body; mailObj.Send(mailMsg); } catch (Exception ex) { System.Web.HttpContext.Current.Response.Write(ex.Message); } }
private static string ExtractImages(string body, ref int count) { int startIndex = body.ToLower().IndexOf(”= 0) { endIndex = body.IndexOf(”\”", startIndex + 10); } else { return “”; } startIndex = startIndex + 10; string imgurl = body.Substring(startIndex, (endIndex – (startIndex))); count = startIndex; return imgurl; } }
No comments:
Post a Comment