System.Net.Mail в ASP.Net 2.0
С помощью классов System.Net.Mail можно будет посылать письма с аттачментами и встроенными картинками.
1 2 3 4 5 6 7 8 9 |
using System.Net.Mail; Attachment attach = new Attachment("MyPic.jpg", System.Net.Mime.MediaTypeNames.Image.Jpeg);
// или так: Attachment attach = new Attachment("MyPic.jpg", "image/jpeg"); MailMessage msg = new MailMessage("from@source.com", "to@destination.com"); msg.Attachments.Add(attach); |
встраивание картинок
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System.Net.Mail; using System.Net.Mime; using System.IO;
[...]
string fromAddress = "from@source.com"; string toAddress = "to@destination.com"; string subject = "Test EmbeddedImage"; string contentId = "image1"; string path = Server.MapPath("~") + "\\"; string filename = path + "MyPicture.jpg"; string body = "Here is a linked resource: <img src=\"cid:image1\"/>";
MailMessage mailMessage = new MailMessage(fromAddress, toAddress); mailMessage.Subject = subject; AlternateView av1 = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); LinkedResource linkedResource = new LinkedResource(filename); linkedResource.ContentId = contentId; linkedResource.ContentType.Name = filename; av1.LinkedResources.Add(linkedResource); mailMessage.AlternateViews.Add(av1); mailMessage.IsBodyHtml = true;
SmtpClient mailSender = new SmtpClient("smtpHost"); try { mailSender.Send(mailMessage); labelStatus.Text = "Message sent!"; } catch { labelStatus.Text = ex.Message; } |