自己编写的C# winForm邮件发送程序发送邮件失败?
发布网友
发布时间:2022-05-10 16:41
我来回答
共4个回答
热心网友
时间:2023-10-16 18:12
界面就不画了,大概的你看一下控件就知道,我这样实验过时成功的呀,对了,不要使用163的邮箱作为发送方,这个会有问题的!
protected void btnSend_Click(object sender, EventArgs e)
{
SmtpClient client = new SmtpClient();
//这个地方的用户名你可以用split从发送人中截取
client.Credentials = new System.Net.NetworkCredential("ggyy-0516", txtPwd.Text);
client.Port = 25;
client.Host = "smtp.126.com";
try
{
client.Send(Initial(txtAddr.Text.ToString()));
ClientScript.RegisterStartupScript(this.GetType(), "myScript", "<script>alert('发送成功!')</script>");
}
catch(System.Net.Mail.SmtpException ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "myScript",
"<script>alert('"+ex.Message.ToString()+"')</script>");
}
}
public MailMessage Initial(string Address)
{
MailMessage m_Mail = new MailMessage();
//发件人
m_Mail.From = new MailAddress(txtSend.Text.Trim());
//收件人
m_Mail.To.Add(new MailAddress(Address));
//主题
m_Mail.Subject = txtTopic.Text;
//内容
m_Mail.Body = txtContent.Text;
//邮件主题和正文编码格式
m_Mail.SubjectEncoding = System.Text.Encoding.UTF8;
m_Mail.BodyEncoding = System.Text.Encoding.UTF8;
//邮件正文是Html编码
m_Mail.IsBodyHtml = true;
//优先级
m_Mail.Priority = System.Net.Mail.MailPriority.High;
//添加附件,可以添加多个
m_Mail.Attachments.Add(new Attachment("f:\\1.txt"));
//密件抄送收件人
m_Mail.Bcc.Add(Address);
//抄送收件人
m_Mail.CC.Add(Address);
return m_Mail;
}
热心网友
时间:2023-10-16 18:13
估计不是你代码问题,要么防火墙问题,要么你的邮箱根本不支持smtp服务。你先验证你邮箱的smtp可用。例如你使用126,几年前的126邮箱可以smtp,现在申请的smtp被封了。
你用下面的写法看看好不好用:
try
{
SmtpClient smtp = new SmtpClient();
smtp.Host = hostTextBox.Text;
if (isAuthCheckBox.Checked)
{
CredentialCache myCache = new CredentialCache();
myCache.Add(hostTextBox.Text, 25, "login", new NetworkCredential(userNameTextBox.Text, passwordTextBox.Text));
smtp.Credentials = myCache;
}
MailMessage message = new MailMessage();
message.Subject = subjectTextBox.Text;
message.Body = bodyTextBox.Text;
message.From = new MailAddress(fromTextBox.Text);
foreach (String to in toTextBox.Text.Split(';'))
{
message.To.Add(to);
}
smtp.Send(message);
MessageBox.Show("No Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
热心网友
时间:2023-10-16 18:13
public void MailSend(String sendTo,String subject,String body)
{
try
{
MailAddress sendfrom = new MailAddress(Common.Class1.sendFromAddress);
MailAddress sendto1 = new MailAddress(sendTo);
MailMessage message = new MailMessage();
message.From = sendfrom;
message.To.Add(sendto1);
message.Subject = subject;
//message.IsBodyHtml = true;
message.Body = body;
SmtpClient sc = new SmtpClient(new String("SMTP服务器地址"));//发送邮件用到的smtp主机(用不同的邮箱,smtp定义则不同)
sc.Credentials = new System.Net.NetworkCredential(new String("邮箱名"), new String("密码"));//邮箱账号与密码
sc.Send(message);//发送邮件
}
catch (SmtpException se)
{
throw new System.Exception(se.ToString());
}
}
热心网友
时间:2023-10-16 18:14
126的smtp跟pop是需要申请开通的,你网页登入后设置里能找到选项