Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created June 20, 2014 09:44
Show Gist options
  • Save jsfaint/53f472327a0a7507352e to your computer and use it in GitHub Desktop.
Save jsfaint/53f472327a0a7507352e to your computer and use it in GitHub Desktop.
/*
* Created by SharpDevelop.
* User: JS
* Date: 2008-10-11
* Time: 22:31
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Net.Mail;
namespace sendmail
{
class Program
{
//要向多个人发送,循环加入 mailMsg.To.Add(new MailAddress(strTo));
//strFrom发件人的邮箱
//strTo收件人的邮箱,支持群发,以";"为分隔
//strSubject邮箱主题
//strContent邮箱的文本内容
//strPwd邮箱密码
//strServerMail 为smtp服务器如Smtp.163.com
public string SendMail(string strFrom, string strTo, string strSubject, string strContent, string strPwd, string strServerMail)
{
if (strFrom == "" || strTo == "" || strSubject == "" || strContent == "" || strPwd == "" || strServerMail == "")
return "各参数不应有空值";
//关于邮件的设置
System.Net.Mail.MailMessage mailMsg = new System.Net.Mail.MailMessage(); //邮件类
mailMsg.From = new MailAddress(strFrom);
string[] toList = strTo.Split(';'); //以";"作为分隔符
foreach (string t in toList)
{
//mailMsg.To.Add(new MailAddress(strTo));
mailMsg.To.Add(new MailAddress(t));
}
mailMsg.Subject = strSubject; //指明主题
mailMsg.Body = strContent; //指明正文
mailMsg.IsBodyHtml = false; //邮件是否为HTML格式
try
{
//用于发送的设置
System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient();
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network; //指明通过smtp方式发送
mailClient.Host = strServerMail; //smtp邮件主机的地址
//安全签名,用于验证的信息
mailClient.Credentials = new System.Net.NetworkCredential(strFrom, strPwd); //指明发送人的身份(发件人邮箱,发件人邮箱密码)
mailClient.Send(mailMsg);
}
catch (SmtpException eError)
{
return eError.Message;
}
return "发送成功!";
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("请输入所要发送的邮件:");
string from = Console.ReadLine();
string to = Console.ReadLine();
string subject = Console.ReadLine();
string connect = Console.ReadLine();
string pwd = Console.ReadLine();
string server = Console.ReadLine();
p.SendMail(from, to, subject, connect, pwd, server);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment