使用.Net自带的邮件类发送邮件报错误
使用.Net自带的邮件类发送邮件报异常
写了一个发送邮件的小程序,之前 一直都可以发送邮件的,今天突然不能发送了,调试了一下发现报异常: InnerException:调用的目标发生异常,求助各位大侠;
写了一个发送邮件的小程序,之前 一直都可以发送邮件的,今天突然不能发送了,调试了一下发现报异常: InnerException:调用的目标发生异常,求助各位大侠;
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration;
namespace WindowsFormsApplication1
{
public class Mail
{
public static bool Send(string to, string title, string content, bool MailServerBodyHtml)
{
try
{
System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
message.To = to;
message.From = "1197312019@qq.com";
message.Subject = title;
message.BodyFormat = System.Web.Mail.MailFormat.Html;
message.BodyEncoding = Encoding.UTF8;
message.Body = content;
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "********@qq.com"); //set your username here
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "*******"); //set your password here
//if (Convert.ToInt32(ConfigurationManager.AppSettings["MailServerPort"].ToString()) != 25)
//{
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//465
message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
//}
System.Web.Mail.SmtpMail.SmtpServer = "smtp@qq.com";//"smtp.qq.com";
System.Web.Mail.SmtpMail.Send(message);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if(Mail.Send("1197312019@qq.com","aaa","aaaa",true)
{
MessageBox.Show("aaaaaaa");
}
else