Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

Attaching Image in the body of mail in C#

How can I attach an image in the body content . I have written the below code

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
string UserName = "[email protected]";
string Password = "my password";
message.To.Add(new System.Net.Mail.MailAddress("[email protected]"));
message.From = new  System.Net.Mail.MailAddress("[email protected]");              
message.Subject = "test subject";
message.Body = "<img src=@'C:\Sunset.jpg'/>";                
message.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
 smtpClient.Host = "hostname";
 smtpClient.Port = 25;
 smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
 smtpClient.Send(message);

The code is fine as I am receiving the message also but the image is coming as [X] inside the body and not as the image. How to solve this? The path is correct?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
    string attachmentPath = Environment.CurrentDirectory + @"est.png";
    Attachment inline = new Attachment(attachmentPath);
    inline.ContentDisposition.Inline = true;
    inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
    inline.ContentId = contentID;
    inline.ContentType.MediaType = "image/png";
    inline.ContentType.Name = Path.GetFileName(attachmentPath);

    message.Attachments.Add(inline);

reference: Send an Email in C# with Inline attachments


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...