Gollum's den

ASP.NET (и не только) здесь и сейчас!

View Eugene Agafonov's profile on LinkedIn

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;
}

По материалам http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1264

Posted: Aug 18 2005, 11:52 AM by Gollum | with 3 comment(s)
Filed under: ,

Comments

Me said:

So how would you do this in .NET 1.0?

It seems Namespace System.NET.MAIL is not available in .NET 1.0...that's why LinkResource and AlternateView cannot be defined...

# November 22, 2006 11:03 PM

Gollum said:

Its not possible, since these classes were implemented only in framework 2.0.

However, If you need the same thing in 1.0, u can either try to decompile System.Net.Mail classes and port them to fw 1.0, or use third-party components.

P.S. Unfortunately I write in russian in this blog, but considering to make it also in english. Until this happens, feel free to ask questions here.

# November 23, 2006 5:52 PM

a said:

Спасибо помог сэкономить время

# January 16, 2008 8:09 AM