c#发送邮件
程序可以运行
没有经过加工的
这是没有经过加工的,看起来比较简单明了。
static void Main(string[] args)
{
SmtpClient mailClient = new SmtpClient("smtp.163.com");//邮箱提供商的SMTP地址,QQ邮箱:smtp.qq.com 163邮箱: smtp.163.com
mailClient.EnableSsl = true;
mailClient.Credentials = new NetworkCredential("发件人邮箱账号", "发件人SMTP密码");//发送者SMTP密码 并非账号登录密码
MailMessage message = new MailMessage(new MailAddress("发件人账号"), new MailAddress("收件人账号"));
//message.Bcc.Add(new MailAddress("收件人账号_1")); //消息群发
//message.Bcc.Add(new MailAddress("收件人账号_2")); //消息群发
//message.Bcc.Add(new MailAddress("收件人账号_3")); //消息群发
message.Body = "Mail Content :I have a GRAPHICS processor, the model is Nvidia Microstar Vantager GTX 1650. ";//邮件内容
message.Subject = "Subject Of An Email";//邮件主题
Attachment att = new Attachment(@"f:/ThisAFile.txt");//附件
message.Attachments.Add(att);//添加附件
Console.WriteLine("U R Sending An Email Plz Hold On");
try
{
mailClient.Send(message); //发送邮件
Console.WriteLine("Congratulations On Successfully Sending An Email");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
Console.ReadLine();
}
上面的程序就会发送一个邮件到收件人的邮箱。
稍微加工一下
需要的信息有:
选择发送人邮箱提供商,暂时提供两个网易,QQ
发送者的邮箱账号,邮箱SMTP密码,收信人的邮箱,
using System;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
namespace SendEmailToQQEmail
{
internal class Program
{
static SMTPType smtpType = SMTPType.WangYi;//选择邮件发送者的服务提供商
static string emailSenderID = "";//发送者的邮箱账号
static string emailSenderPassword = "";//发送者的SMTP密码
static string mailReceiverEmail = "";//接收者的账号
static string mailMessageContent = "Mail Content :I have a GRAPHICS processor, the model is Nvidia Microstar Vantager GTX 1650. ";//信息内容
static string mailMessageTitle = "My computer configuration";//信息标题
static string mailAttachmentPanth = @"";//附件路径
static void Main(string[] args)
{
if (!CheckEmailInfo())
{
Console.ReadKey();
return;
}
SmtpClient mailClient = emailSMTPClient(HostPort(smtpType));//我其实不想使用方法套着方法的,但是我暂时么有掌握使用枚举点出我这个方法,所以暂时使用方法套方法了。
mailClient.EnableSsl = true;
mailClient.Credentials = new NetworkCredential(emailSenderID, emailSenderPassword);//发送者SMTP密码 并非账号登录密码
MailMessage message = new MailMessage(new MailAddress(emailSenderID), new MailAddress(mailReceiverEmail));
message.Body = mailMessageContent;//邮件内容
message.Subject = mailMessageTitle;//邮件主题
if (mailAttachmentPanth != "")
{
Console.WriteLine("No attachment is selected to send......");
Attachment att = new Attachment(mailAttachmentPanth);//附件
message.Attachments.Add(att);//添加附件
}
Console.WriteLine("Please wait while you are sending an email.....");
try
{
mailClient.Send(message); //发送邮件
Console.WriteLine("Congratulations on your successful E-mail!!!!");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
Console.WriteLine("Exit with space after the email is sent....");
Console.ReadLine();
}
static bool CheckEmailInfo()
{
if (emailSenderID == "")
{
Console.WriteLine("Please enter the sender's account in the code!!!");
return false;
}
if (emailSenderPassword == "")
{
Console.WriteLine("Please enter the sender's password in the code!!!");
return false;
}
if (mailReceiverEmail == "")
{
Console.WriteLine("Please enter the email recipient's account number in the code!!!");
return false;
}
return true;
}
//根据邮箱服务提供商的类型创建 邮件发送者对象
static SmtpClient emailSMTPClient(SMTPType_HostPort smtpType)
{
SmtpClient mailClient = new SmtpClient(smtpType.mailServiceProviderSMTPHost, int.Parse(smtpType.mailServiceProviderSMTPPort));//邮箱提供商的SMTP地址,QQ邮箱:smtp.qq.com 163邮箱: smtp.163.com
return mailClient;
}
//获取邮箱服务提供商的host 以及端口号
//参数是邮箱服务提供商
public static SMTPType_HostPort HostPort(SMTPType smtpType)
{
SMTPType_HostPort SMTPType_HostPort;
switch (smtpType)
{
case SMTPType.QQ:
SMTPType_HostPort = new SMTPType_HostPort()
{
mailServiceProviderSMTPHost = "smtp.qq.com",
mailServiceProviderSMTPPort = "25"
};
return SMTPType_HostPort;
break;
case SMTPType.WangYi:
SMTPType_HostPort = new SMTPType_HostPort()
{
mailServiceProviderSMTPHost = "smtp.163.com",
mailServiceProviderSMTPPort = "25"
};
return SMTPType_HostPort;
break;
default:
break;
}
MessageBox.Show("Error: the email provider's host and IP were not obtained", "Program Error", MessageBoxButtons.OK);
return null;
}
//邮箱服务提供商
public enum SMTPType
{
QQ,
WangYi,
}
}
//邮箱服务提供商的 信息
public class SMTPType_HostPort
{
/// <summary>
/// 邮件服务提供商的host
/// </summary>
public string mailServiceProviderSMTPHost;
/// <summary>
/// 邮件服务提供商的端口号
/// </summary>
public string mailServiceProviderSMTPPort;
}
}
网易邮箱开启SMTP服务
部分错误解析
使用第一种方式可能会出现的错误
错误一
没有发送者的用户名
错误二
没有发送者的密码,发送者的密码不是邮箱账户的密码,而是SMTP的密码,置于怎么获得,点击我查看SMTP密码
错误三
邮箱服务提供商的host没有写对
网易163:smtp.163.com
QQ:smtp.qq.com
错误四
附件部分的代码没有注释,
错误无
检查网路连接,需要联网
使用第二种方式可能会出现的错误
错误一
检查网路连接,需要联网
错误二
暂时么有遇到错误,因为就测试了两次