如何将图像作为附件发送到c#?
答案 0 :(得分:1)
public static void SendMail(string email, string message)
{
// Algorithm :: A MailMessage object is created and the details such as FromAddress, ToAddress, Subject, Body are included and the mail is sent.
MailMessage newmessage = new MailMessage();
try
{
newmessage.From = new MailAddress("email", "subject");
newmessage.To.Add(new MailAddress(email));
newmessage.Subject = "Hii Friends..";
newmessage.Body += message;
newmessage.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
//SmtpClient client = new SmtpClient("smtp.mail.yahoo.com");
//client.Port = 587; yahoo port number
client.EnableSsl = true;
string path = System.Web.HttpContext.Current.Server.MapPath(@"logo.png");
AlternateView plainView = AlternateView.CreateAlternateViewFromString("This is my text , viewable by those clients that don't support html", null, "text/plain");
LinkedResource logo = new LinkedResource(path);
logo.ContentId = "companylogo";
AlternateView av1 = AlternateView.CreateAlternateViewFromString("<html><body><img src=cid:companylogo/><br></body></html>" + message, null, MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);
newmessage.AlternateViews.Add(plainView);
newmessage.AlternateViews.Add(av1);
if (!string.IsNullOrEmpty("mail"))
{
client.Credentials = new NetworkCredential("mail", "password");
}
client.Send(newmessage);
}
catch
{
}
return;
}
答案 1 :(得分:0)
你可以这样做,
System.Net.Mail.MailMessage MyMessage = new System.Net.Mail.MailMessage();
MyMessage.From = new MailAddress(“EmailFrom”);
MyMessage.To.Add(new MailAddress(“ToEmail”));
MyMessage.Subject = “EmailSubject”;
MyMessage.Body = “EmailBody”;
Attachment DailyAttachFile = new Attachment(“Full Image Path”);
MyMessage.Attachments.Add(DailyAttachFile);
SmtpClient emailClient = new SmtpClient(“SMTPServer”);
emailClient.Port = int.Parse(“SMTPServerPortNo”);
emailClient.Send(MyMessage);
MyMessage.Dispose();