C# 如何在asp.net中使用smtp发送邮件
我运行时出现了一些错误:发送电子邮件失败。 SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.0必须首先发出STARTTLS命令。i1sm8651517pbj.70C# 如何在asp.net中使用smtp发送邮件,c#,asp.net,email,smtp,C#,Asp.net,Email,Smtp,我运行时出现了一些错误:发送电子邮件失败。 SMTP服务器需要安全连接,或者客户端未通过身份验证。服务器响应为:5.7.0必须首先发出STARTTLS命令。i1sm8651517pbj.70 i need solution for this error 使用系统; 使用系统配置; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.WebControl; 使用System.Web.UI.HTMLContr
i need solution for this error
使用系统;
使用系统配置;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.HTMLControl;
使用System.Net.Mail;
公共部分类\u默认值:System.Web.UI.Page
{
#地区“发送电子邮件”
受保护的无效btnSendmail\u单击(对象发送者,事件参数e)
{
//System.Web.Mail.SmtpMail.SmtpServer在2.0中已过时
//System.Net.Mail.SmtpClient是2.0中的替代类
SmtpClient SmtpClient=新的SmtpClient();
MailMessage=新的MailMessage();
尝试
{
MailAddress fromAddress=新邮件地址(txtmail.Text,txtName.Text);
//您可以指定服务器的主机名或IP地址
//IIS中的默认值将是localhost
smtpClient.Host=“smtp.gmail.com”;
//默认端口为25
smtpClient.Port=587;
//发件人地址将作为MailAddress对象提供
message.From=fromAddress;
//处理邮件地址集合的步骤
message.To.Add(“muthu17green@gmail.com");
message.Subject=“反馈”;
//抄送和密件抄送可选
//MailAddressCollection类用于向各种用户发送电子邮件
//您可以将地址指定为新邮件地址(“admin1@yoursite.com")
message.CC.Add(“muthu17green@gmail.com");
message.CC.Add(“muthu17green@gmail.com");
//您可以直接将地址指定为字符串
message.Bcc.Add(新邮件地址(“muthu17green@gmail.com"));
message.Bcc.Add(新邮件地址(“muthu17green@gmail.com"));
//正文可以是Html或文本格式
//如果是html消息,请指定true
message.IsBodyHtml=false;
//消息正文内容
message.Body=txtMessage.Text;
//发送SMTP邮件
发送(消息);
lblStatus.Text=“电子邮件已成功发送。”;
}
捕获(例外情况除外)
{
lblStatus.Text=“发送电子邮件失败。
”+例如消息;
}
}
#端区
#区域“重置”
受保护的无效btnReset_单击(对象发送者,事件参数e)
{
txtName.Text=“”;
txtMessage.Text=“”;
txtEmail.Text=“”;
}
#端区
}
您需要设置SmtpClient.Credentials
属性:
using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
public partial class _Default : System.Web.UI.Page
{
#region "Send email"
protected void btnSendmail_Click(object sender, EventArgs e)
{
// System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
// System.Net.Mail.SmtpClient is the alternate class for this in 2.0
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
// You can specify the host name or ipaddress of your server
// Default in IIS will be localhost
smtpClient.Host = "smtp.gmail.com";
//Default port will be 25
smtpClient.Port = 587;
//From address will be given as a MailAddress Object
message.From = fromAddress;
// To address collection of MailAddress
message.To.Add("muthu17green@gmail.com");
message.Subject = "Feedback";
// CC and BCC optional
// MailAddressCollection class is used to send the email to various users
// You can specify Address as new MailAddress("admin1@yoursite.com")
message.CC.Add("muthu17green@gmail.com");
message.CC.Add("muthu17green@gmail.com");
// You can specify Address directly as string
message.Bcc.Add(new MailAddress("muthu17green@gmail.com"));
message.Bcc.Add(new MailAddress("muthu17green@gmail.com"));
//Body can be Html or text format
//Specify true if it is html message
message.IsBodyHtml = false;
// Message body content
message.Body = txtMessage.Text;
// Send SMTP mail
smtpClient.Send(message);
lblStatus.Text = "Email successfully sent.";
}
catch (Exception ex)
{
lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
}
}
#endregion
#region "Reset"
protected void btnReset_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtMessage.Text = "";
txtEmail.Text = "";
}
#endregion
}
这是用于验证以发送消息的内容。您可能还需要确保启用SSL:
smtpClient.Credentials = new NetworkCredentials("yourUserName", "yourPassword");
您似乎正在尝试使用
GMail
发送电子邮件,这需要SSL
看这个 因此,在web.config中,通过以下方式启用SSL:
smtpClient.EnableSsl = true;
你可以查看我的帖子,了解如何使用gmail autentication发送电子邮件
我想你忘了将
enablesssl
属性设置为true
,这是gmail所必需的
以下是示例代码:
smtpClient.EnableSsl = true;
使用C#Asp.NET从GODADDY发送带有附件的电子邮件您可以在回答中为代码添加注释和解释。您的回答不适用于Gmail,也无法解决问题中的错误。
smtpClient.EnableSsl = true;
protected void btnSend_Click(object sender, EventArgs e)
{
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtFrom.Text);
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtBody.Text;
SmtpClient mySmtpClient = new SmtpClient();
System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(txtFrom.Text,txtPwd.Text);
mySmtpClient.Host = "smtp.gmail.com";
mySmtpClient.Port=587;
mySmtpClient.EnableSsl = true;
mySmtpClient.UseDefaultCredentials = false;
mySmtpClient.Credentials = myCredential;
mySmtpClient.Send(msg);
msg.Dispose();
lberr.Text="Message sent successfully";
clrtxt();
}
catch(SmtpException)
{
lberr.Text="SMTP Error handled";
}
}
public void SendMail()
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(MailTo.Text);
mail.From = new MailAddress(MailFrom.Text,"Invoice");
mail.Subject = Subject.Text;
mail.Body = Body.Text;
mail.IsBodyHtml = true;
string FileName = Path.GetFileName(FileUploadAttachments.PostedFile.FileName);
Attachment attachment = new Attachment(FileUploadAttachments.PostedFile.InputStream ,FileName);
mail.Attachments.Add(attachment);
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential("Your_Email@Email.com", "Your_Email_Password");
client.Host = "smtpout.secureserver.net";
client.Port = 80;
try
{
client.Send(mail);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}