简体   繁体   中英

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. 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....

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 :

<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. The server response was: 5.5.1 Authentication

Its a security issue, Gmail by default prevents access for your mail account from custom applications. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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