自己编写的C# winForm邮件发送程序发送邮件失败?
是不是在发送之前要配制什么东东。代码由下://发送按钮的单击事件privatevoidbtn_Begin_Click(objectsender,EventArgse){s...
是不是在发送之前要配制什么东东。代码由下:
//发送按钮的单击事件
private void btn_Begin_Click(object sender, EventArgs e)
{
string from= txt_from.Text.Trim();//发送账号
string psd = txt_from_psd.Text.Trim();//密码
string to = txt_to.Text.Trim();//收件账号
string title = txt_title.Text;
string content = txt_content.Text;
string[] fasong = from.Split('@');
string[] fs = fasong[1].Split('.');
//发送
SmtpClient client = new SmtpClient("smtp." + fs[0].ToString() + ".com"); //设置邮件协议
client.UseDefaultCredentials = false;//
client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器
client.Credentials = new NetworkCredential(fasong[0].ToString(), psd); //通过用户名和密码 认证
MailMessage mmsg = new MailMessage(new MailAddress(from), new MailAddress(to)); //发件人和收件人的邮箱地址
mmsg.Subject = title; //邮件主题
mmsg.SubjectEncoding = Encoding.UTF8; //主题编码
mmsg.Body = content; //邮件正文
mmsg.BodyEncoding = Encoding.UTF8; //正文编码
mmsg.IsBodyHtml = true; //设置为HTML格式
mmsg.Priority = MailPriority.High; //优先级
try
{
client.Send(mmsg);
MessageBox.Show("邮件已发成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
我是用sina邮件测试发送的,但还是发送失败。哪位大哥能告诉我,我第二次补充了,哪位大哥能帮忙解知之一下。 展开
//发送按钮的单击事件
private void btn_Begin_Click(object sender, EventArgs e)
{
string from= txt_from.Text.Trim();//发送账号
string psd = txt_from_psd.Text.Trim();//密码
string to = txt_to.Text.Trim();//收件账号
string title = txt_title.Text;
string content = txt_content.Text;
string[] fasong = from.Split('@');
string[] fs = fasong[1].Split('.');
//发送
SmtpClient client = new SmtpClient("smtp." + fs[0].ToString() + ".com"); //设置邮件协议
client.UseDefaultCredentials = false;//
client.DeliveryMethod = SmtpDeliveryMethod.Network; //通过网络发送到Smtp服务器
client.Credentials = new NetworkCredential(fasong[0].ToString(), psd); //通过用户名和密码 认证
MailMessage mmsg = new MailMessage(new MailAddress(from), new MailAddress(to)); //发件人和收件人的邮箱地址
mmsg.Subject = title; //邮件主题
mmsg.SubjectEncoding = Encoding.UTF8; //主题编码
mmsg.Body = content; //邮件正文
mmsg.BodyEncoding = Encoding.UTF8; //正文编码
mmsg.IsBodyHtml = true; //设置为HTML格式
mmsg.Priority = MailPriority.High; //优先级
try
{
client.Send(mmsg);
MessageBox.Show("邮件已发成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
我是用sina邮件测试发送的,但还是发送失败。哪位大哥能告诉我,我第二次补充了,哪位大哥能帮忙解知之一下。 展开
4个回答
展开全部
界面就不画了,大概的你看一下控件就知道,我这样实验过时成功的呀,对了,不要使用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;
}
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;
}
Zoho Mail
2024-11-18 广告
2024-11-18 广告
作为卓迈(北京)技术有限公司的工作人员,我们推荐使用Gmail、Outlook等国际知名的电子邮箱服务来在国外收发邮件。这些邮箱服务拥有广泛的国际覆盖和稳定的性能,能确保您与全球各地的合作伙伴和客户保持顺畅的沟通。同时,它们也具备强大的垃圾...
点击进入详情页
本回答由Zoho Mail提供
展开全部
估计不是你代码问题,要么防火墙问题,要么你的邮箱根本不支持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);
}
你用下面的写法看看好不好用:
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);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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());
}
}
{
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());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
126的smtp跟pop是需要申请开通的,你网页登入后设置里能找到选项
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询