using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool a = SendEmail();
}
public bool SendEmail()
{
try
{
string sjr = "";//收件人地址
string fjr = "";//发件人地址
string Hostdy = "";//SMTP服务器地址
string Portdy = "";//SMTP端口,QQ邮箱填写587
string sqm = ""; //授权码
sjr = textBox1.Text.ToString();
fjr = textBox2.Text.ToString();
Hostdy = textBox3.Text.ToString();
Portdy = textBox4.Text.ToString();
sqm = textBox5.Text.ToString();
MailMessage msg = new MailMessage();
msg.To.Add(sjr);//收件人地址
msg.From = new MailAddress(fjr, "Edward");//发件人邮箱,名称
msg.Subject = "This is a test email from QQ";//邮件标题
msg.SubjectEncoding = Encoding.UTF8;//标题格式为UTF8
msg.Body = "this is body";//邮件内容
msg.BodyEncoding = Encoding.UTF8;//内容格式为UTF8
SmtpClient client = new SmtpClient();
client.Host = Hostdy;//SMTP服务器地址
client.Port = int.Parse(Portdy);//SMTP端口,QQ邮箱填写587
client.EnableSsl = false;//启用SSL加密
//发件人邮箱账号,授权码(注意此处,是授权码你需要到qq邮箱里点设置开启Smtp服务,然后会提示你第三方登录时密码处填写授权码)
client.Credentials = new System.Net.NetworkCredential(fjr, sqm);
client.Send(msg);//发送邮件
}
catch (Exception ex)
{
WriteLog(ex);
return false;
}
return true;
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
public static void WriteLog(Exception ex, string LogAddress = "")
{
//如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
if (LogAddress == "")
{
LogAddress = Environment.CurrentDirectory + '\\' +
DateTime.Now.Year + '-' +
DateTime.Now.Month + '-' +
DateTime.Now.Day + "_Log.log";
}
//把异常信息输出到文件
StreamWriter sw = new StreamWriter(LogAddress, true);
sw.WriteLine("当前时间:" + DateTime.Now.ToString());
sw.WriteLine("异常信息:" + ex.Message);
sw.WriteLine("异常对象:" + ex.Source);
sw.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
sw.WriteLine("触发方法:" + ex.TargetSite);
sw.WriteLine();
sw.Close();
}
}
}
直接放代码和错误,
我这个代码是自己做的测试的,外网发外网,用QQ邮箱是没问题的,
但是在客户服务器上就是走不通,客户给的邮箱,在服务器上是能PING通的,
也开放了入站出站策略。
邮箱是企业自建的邮箱,据说是邮箱只有一个密码,没有授权码,客户之前的系统,都是用java搭建的,用此密码能发送成功
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getInstance(pro,authenticator);
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址,并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO,to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
有没有其他的发送方式