欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net SMTP发送Email实例(可带附件)

程序员文章站 2024-03-02 08:31:22
复制代码 代码如下: public static void sendemail(string toaddress, string emailbody) { var from...
复制代码 代码如下:

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"/>
<--带附件版本->
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();
//如果没有路径,用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);