SMTP email from C#
程序员文章站
2022-06-19 17:00:35
/// /// 一人一附件发送邮件 /// 2017-05-17 涂聚文 GeovinDu /// /// 接收邮件人 /// 邮件标题 /// 邮件内容 /// 是否網頁 /// /// 附件 /// ... ......
/// <summary>
/// 一人一附件发送邮件
/// 2017-05-17 涂聚文 geovindu
/// </summary>
/// <param name="to">接收邮件人</param>
/// <param name="subject">邮件标题</param>
/// <param name="msg">邮件内容</param>
/// <param name="bodyhtml">是否網頁</param>
/// <param name="attachmentstream"></param>
/// <param name="attachmentfilename">附件</param>
/// <returns></returns>
public static bool sendemaildu(string to, string subject, string msg, bool bodyhtml, memorystream attachmentstream,string attachmentfilename)
{
bool sendok = false;
try
{
system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
newmsg.bodyencoding = system.text.encoding.utf8;
newmsg.headersencoding = system.text.encoding.utf8;
newmsg.subjectencoding = system.text.encoding.utf8;
system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
//smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
//smtpclient.port = 25;
//smtpclient.enablessl = false;
if (attachmentstream != null && attachmentfilename != null)
{
// add an attachment.
//string sfile = @"c:\source\lns_is.txt";
//mailattachment oattch = new mailattachment(sfile, mailencoding.base64);
//string filepath = @"c:\testemail\skype11月黑咭生日會員.xls";
//memorystream memstream = new memorystream();
//using (filestream filestream = file.openread(filepath))
//{
// memstream.setlength(filestream.length);
// filestream.read(memstream.getbuffer(), 0, (int)filestream.length);
//}
newmsg.attachments.add(new attachment(attachmentstream, attachmentfilename)); //mediatypenames.image.jpeg)
//system.net.mail.attachment attachment = new system.net.mail.attachment(attachmentstream, attachmentfilename);
//system.net.mime.contentdisposition disposition = attachment.contentdisposition;
//disposition.filename = attachmentfilename;
//disposition.dispositiontype = system.net.mime.dispositiontypenames.attachment;
//newmsg.attachments.add(attachment);
}
newmsg.isbodyhtml = bodyhtml;
smtpclient.send(newmsg);
sendok = true;
//return sent_ok;
}
catch (exception ex)
{
ex.message.tostring();
attachmentstream.close();
sendok = false;
//"error: " + ex.message + "<br/><br/>inner exception: "+ ex.innerexception;
}
attachmentstream.close();
return sendok;
}
/// <summary>
/// 一人一附件发送邮件
/// 2017-05-17 涂聚文 geovindu
/// </summary>
/// <param name="to">接收邮件人</param>
/// <param name="subject">邮件标题</param>
/// <param name="msg">邮件内容</param>
/// <param name="bodyhtml">是否網頁</param>
/// <param name="attachmentfilename">附件</param>
/// <returns></returns>
public static bool sendemaildu(string to, string subject, string msg, bool bodyhtml, string attachmentfilename)
{
memorystream attachmentstream = new memorystream();
bool sendok = false;
try
{
system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
newmsg.bodyencoding = system.text.encoding.utf8;
newmsg.headersencoding = system.text.encoding.utf8;
newmsg.subjectencoding = system.text.encoding.utf8;
system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
//smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
//smtpclient.port = 25;
//smtpclient.enablessl = false;
if (attachmentstream != null && attachmentfilename != null)
{
// add an attachment.
//string sfile = @"c:\source\lns_is.txt";
//mailattachment oattch = new mailattachment(sfile, mailencoding.base64);
//string filepath = @"c:\testemail\skype11月黑咭生日會員.xls";
//memorystream memstream = new memorystream();
//using (filestream filestream = file.openread(filepath))
//{
// memstream.setlength(filestream.length);
// filestream.read(memstream.getbuffer(), 0, (int)filestream.length);
//}
newmsg.attachments.add(new attachment(attachmentstream, attachmentfilename)); //mediatypenames.image.jpeg)
//system.net.mail.attachment attachment = new system.net.mail.attachment(attachmentstream, attachmentfilename);
//system.net.mime.contentdisposition disposition = attachment.contentdisposition;
//disposition.filename = attachmentfilename;
//disposition.dispositiontype = system.net.mime.dispositiontypenames.attachment;
//newmsg.attachments.add(attachment);
}
newmsg.isbodyhtml = bodyhtml;
smtpclient.send(newmsg);
sendok = true;
//return sent_ok;
}
catch (exception ex)
{
ex.message.tostring();
attachmentstream.close();
sendok = false;
//"error: " + ex.message + "<br/><br/>inner exception: "+ ex.innerexception;
}
attachmentstream.close();
return sendok;
}
/// <summary>
/// 一人多附件发送邮件
/// 2019-06-18 涂聚文 geovindu
/// </summary>
/// <param name="to">接收邮件人</param>
/// <param name="subject">邮件标题</param>
/// <param name="msg">邮件内容</param>
/// <param name="bodyhtml">是否網頁</param>
/// <param name="attachmentfiles">多附件</param>
/// <returns></returns>
public static bool sendemaildufiles(string to, string subject, string msg, bool bodyhtml, list<string> attachmentfiles)
{
memorystream attachmentstream = new memorystream();
bool sendok = false;
try
{
system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
newmsg.bodyencoding = system.text.encoding.utf8;
newmsg.headersencoding = system.text.encoding.utf8;
newmsg.subjectencoding = system.text.encoding.utf8;
system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
//smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
//smtpclient.port = 25;
//smtpclient.enablessl = false;
if (attachmentstream != null && attachmentfiles != null)
{
foreach (string filename in attachmentfiles)
{
newmsg.attachments.add(new attachment(attachmentstream, filename)); //mediatypenames.image.jpeg)
}
}
newmsg.isbodyhtml = bodyhtml;
smtpclient.send(newmsg);
sendok = true;
}
catch (exception ex)
{
ex.message.tostring();
attachmentstream.close();
sendok = false;
}
attachmentstream.close();
return sendok;
}
/// <summary>
/// 多人多附件发送邮件
/// 不考虑统计每封邮件发送状态(另外考虑完善代码)
/// 2019-06-18 涂聚文 geovindu
/// </summary>
/// <param name="tos">多人接收邮件人</param>
/// <param name="subject">邮件标题</param>
/// <param name="msg">邮件内容</param>
/// <param name="bodyhtml">是否網頁</param>
/// <param name="attachmentfiles">多附件</param>
/// <returns></returns>
public static bool sendemailsandfiles(list<string> tos, string subject, string msg, bool bodyhtml, list<string> attachmentfiles)
{
memorystream attachmentstream = new memorystream();
bool sendok = false;
list<attachment> all = new list<attachment>();
try
{
if (attachmentstream != null && attachmentfiles != null)
{
foreach (string filename in attachmentfiles)
{
all.add(new attachment(attachmentstream, filename));
}
}
if (tos.count > 0)
{
foreach (string to in tos)
{
system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
newmsg.bodyencoding = system.text.encoding.utf8;
newmsg.headersencoding = system.text.encoding.utf8;
newmsg.subjectencoding = system.text.encoding.utf8;
system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
//smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
//smtpclient.port = 25;
//smtpclient.enablessl = false;
if (attachmentstream != null && attachmentfiles != null)
{
foreach (attachment attname in all)
{
newmsg.attachments.add(attname); //mediatypenames.image.jpeg)
}
}
newmsg.isbodyhtml = bodyhtml;
smtpclient.send(newmsg);
sendok = true;
}
}
}
catch (exception ex)
{
ex.message.tostring();
attachmentstream.close();
sendok = false;
}
attachmentstream.close();
return sendok;
}
上一篇: python-字典dict和集合set
下一篇: c++实现一个简易的网络缓冲区的实践
推荐阅读
-
C#实现将Email地址转成图片显示的方法
-
C# Email邮件发送功能 找回或重置密码功能
-
C# regular expression to validate email
-
C# copy files from source directory to destination file and rename repeated files and does not override
-
C# extract img url from web content then download the img
-
C# SMTP发送邮件的示例
-
C# extract multiples from web pages based on OpenQA.Selenium.Chrome and ChromeDriver
-
c#判断email地址是否为合法
-
C#实现将Email地址转成图片显示的方法
-
C# regular expression to validate email