简体   繁体   English

在c#中从smtp gmail发送电子邮件

[英]sending email from smtp gmail in c#

i am trying to send email from my web app. 我正在尝试从我的网络应用程序发送电子邮件。 i am using smtp gmail for the same. 我正在使用smtp gmail but for some reasons i am not able to send a mail.. i have tried port numbers like 25,465,587 but none of these are working.... 但由于某些原因,我无法发送邮件..我尝试过端口号码,如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>");
        }
    }

calling this method: 调用此方法:

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 settings : 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>

Exception : The SMTP server requires a secure connection or the client was not authenticated. 例外:SMTP服务器需要安全连接或客户端未经过身份验证。 The server response was: 5.5.1 Authentication 服务器响应是:5.5.1身份验证

Its a security issue, Gmail by default prevents access for your mail account from custom applications. 这是一个安全问题,默认情况下Gmail会阻止您从自定义应用程序访问您的邮件帐户。 You can set it up to accept the login from your application. 您可以将其设置为接受应用程序的登录。 在此输入图像描述 After Logging in to your mail , 登录邮件后,

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

Update: 更新:

 msg.IsBodyHtml = true;

Try this:- 尝试这个:-

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

        sendmail(recieverID, subject, body);
    }

method for sending mail 发送邮件的方法

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 要么

U can also go through this link 你也可以通过这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM