smtp发送带附件的邮件代码分享
程序员文章站
2024-02-17 23:39:04
该方式直接保存为html文件,也可以是文本文件,其它格式效果不是很好
复制代码 代码如下:
...
该方式直接保存为html文件,也可以是文本文件,其它格式效果不是很好
复制代码 代码如下:
mailmessage mmsg = new mailmessage();
mmsg.subject = "邮件标题";
mmsg.body = "邮件内容";
mmsg.to.add("接收邮箱
byte[] bytes = system.text.encoding.default.getbytes
(@"<table><tr><td width=150>1234567891234567
</td><td width=80>12345678</td></tr></table>");
memorystream ms = new memorystream(bytes);
contenttype ct = new contenttype();
//附件文件类型
ct.mediatype = mediatypenames.text.html;
//附件名称,可以是其它后缀名
ct.name = "附件名称" + datetime.now.tostring() + ".html";
mmsg.attachments.add(new attachment(ms, ct));
//smtp简单邮件协议
system.net.mail.smtpclient sc
= new system.net.mail.smtpclient();
sc.host = "127.0.0.1";//主机地址
sc.port = 25;//端口
//发送邮箱账号和密码
sc.credentials =
new system.net.networkcredential("account", "password");
//发送邮箱
mmsg.from = new mailaddress("");
sc.send(mmsg);
//释放流资源
ms.close();
ms.dispose();
另附上一个.net使用smtp发送带附件邮件的实例
复制代码 代码如下:
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 = "";
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, "");
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);