从c#

时间:2016-05-07 06:35:42

标签: c# asp.net email smtp smtpclient

我正在尝试从我的网络应用发送电子邮件。我正在使用smtp gmail。但由于某些原因,我无法发送邮件..我尝试过25,465,587这样的端口号,但这些都没有工作....

public static void SendMail(string host, string senderName, string frmAddress, string toAddress, int port, string subject, string messageText)
    {
        String smtpHost, port1;

        smtpHost = ConfigurationManager.AppSettings["smtphost"].ToString();
        port1 = ConfigurationManager.AppSettings["smtpport"].ToString();

        SmtpClient mailClient = new SmtpClient(smtpHost, Convert.ToInt16(port1));
        mailClient.EnableSsl = true;


        NetworkCredential cred = new NetworkCredential();
        cred.UserName = ConfigurationManager.AppSettings["username"].ToString();
        cred.Password = ConfigurationManager.AppSettings["password"].ToString();
        mailClient.Credentials = cred;

        mailClient.UseDefaultCredentials = false;
        //mailClient.Credentials = cred;

        System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
        try
        {
            MailAddress fromAddress = new MailAddress(frmAddress, senderName);
            message.From = fromAddress;

            message.To.Add(toAddress);
            message.Subject = subject;

            message.IsBodyHtml = false;
            message.Body = messageText;

            mailClient.Send(message);
            //Response.Write("<script  language='javascript' type='text/javascript'>window.alert('Email Successfully Sent.');</script>");
        }
        catch
        {
            //Response.Write("<script  language='javascript' type='text/javascript'>window.alert('Send Email Failed '" + ex.Message + ");</script>");
        }
    }

调用此方法:

protected void btnMail_Click1(object sender, EventArgs e)
    {
        string from = txtEmail.Text;
        string to = "my gmail id";
        string message = taMessage.InnerText;

        Global.SendMail(ConfigurationManager.AppSettings["smtphost"].ToString(), txtName.Text, from, to, Convert.ToInt32(ConfigurationManager.AppSettings["smtpport"]), "Enquiry", taMessage.InnerText);
    }

web.config设置:

<appSettings>
    <add key="smtphost" value="smtp.gmail.com"/>
    <!--<add key="smtphost" value="103.231.41.229"/>-->
    <add key="smtpport" value="587"/>
    <!--<add key="port" value="25"/>-->
    <add key="username" value="my gmail id/>
    <add key="password" value="my password"/>
    <add key="From" value="my gmail id"/>
  </appSettings>

例外:SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.5.1身份验证

2 个答案:

答案 0 :(得分:2)

这是一个安全问题,Gmail默认阻止您从自定义应用程序访问您的邮件帐户。您可以将其设置为接受应用程序的登录。 enter image description here 登录邮件后

https://www.google.com/settings/security/lesssecureapps

更新

 msg.IsBodyHtml = true;

答案 1 :(得分:1)

试试这个: -

protected void Button1_Click(object sender, EventArgs e)
    {
        // body
        //subject
        //reciever id

        sendmail(recieverID, subject, body);
    }

发送邮件的方法

protected void sendmail(string reciever, string subject, string body)
    {
        string senderID = "sender email id";
        string pwd = "password ";
        string result = "Message Sent Successfully";
        try
        {
            SmtpClient smtpClient = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
                Credentials = new System.Net.NetworkCredential(senderID, pwd),
                Timeout = 30000
            };
            MailMessage msg = new MailMessage(senderID, reciever, subject, body);
            msg.IsBodyHtml = true;
            smtpClient.Send(msg);
        }
        catch
        {
            result = "Error Sending Mail";
        }
       Response.Write(result );
    }

OR

你也可以通过这个link