This article gives you a detailed introduction of the following. NET SMTP send email with attachment to the specific implementation of ideas and code, want to realize the friend can refer to Ha, I hope to help you
copy code code as follows:
public static void SendEmail (String toaddress, string emailbody)
{
var fromaddress = configurationmanager.appsettings["EmailAddress"];
string frompassword = configurationmanager.appsettings["Emailpassword"]. ToString ();
Const String Subject = "Job Recommendation";
var smtp = new SmtpClient
{
Host = configurationmanager.appsettings["SmtpServer"]. ToString (),
Port = Int. Parse (configurationmanager.appsettings["Smtpport"]),
Enablessl = True,
Deliverymethod = smtpdeliverymethod.network,
useDefaultCredentials = False,
Credentials = new NetworkCredential (fromaddress, Frompassword)
};
using (var message = new MailMessage (fromaddress, toaddress, subject, Httputility.htmlencode (emailbody))
{
SMTP. Send (message);
}
}
<add key= "EmailAddress" value= "********** @gmail. com"/>//email address
<add key= "Emailpassword" value= "*********"/>//emial PWD,
<add key= "smtpserver" value= smtp.gmail.com "/>"
<add key= "Smtpport" value= 587 "/>"
<--with attachment version->
var fromaddress = "allenyinj@gmail.com";
string frompassword = "yj1989120";
Const String subject = "CV";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
Enablessl = True,
Deliverymethod = smtpdeliverymethod.network,
useDefaultCredentials = False,
Credentials = new NetworkCredential (fromaddress, Frompassword)
};
mailmessage email=new mailmessage (fromaddress, "allen.yin.jun@gmail.com");
email. Subject = "INLINE attachment TEST";
email. Isbodyhtml = true;
string AttachmentPath = "C:3.jpeg";
Attachment inline = new Attachment (AttachmentPath);
Inline. Contentdisposition.inline = true;
Inline. Contentdisposition.dispositiontype = Dispositiontypenames.inline;
//inline. ContentID = "1";
//inline. Contenttype.mediatype = "Image/png";
Inline. Contenttype.name = Path.getfilename (AttachmentPath);
email. Attachments.Add (inline);
email. BODY = "Test";
SMTP. Send (email);
email. Dispose ();
//If there is no path, use stream
Attachment letter = new Attachment (fileuploadletter.filecontent, FileUploadLetter.PostedFile.ContentType);
letter. Contentdisposition.inline = true;
letter. Contentdisposition.dispositiontype = Dispositiontypenames.inline;
//inline. ContentID = "1";
letter. Contenttype.mediatype = FileUploadLetter.PostedFile.ContentType;
letter. Contenttype.name = Path.getfilename (FileUploadLetter.PostedFile.FileName);
letter. Name = Path.getfilename (FileUploadLetter.PostedFile.FileName);