Reg:使用asp.net c#从gmail发送的邮件

问题描述:

朋友们,
使用asp.net C#发送邮件时出现问题.出现错误,例如发送邮件失败.

我尝试将端口号分别设置为25和587.
这是我的代码

Hi friends,
I have problem while sending a mail using asp.net C#. got an error like failure sending mail.

i was tried port numbers of 25 and 587 both.
This is my code

public bool sendMail(string mailID, string srvMailID, string srvMailPassword, string dispName, string mailSubject, string smtpHost, string mailDescription, string port)
    {
        bool sent = false;

        try
        {
            string frmAddress = srvMailID.ToString();
            string toAddress = mailID.ToString();


            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

            mail.To.Add(toAddress);

            mail.From = new System.Net.Mail.MailAddress(srvMailID, dispName.ToString(), System.Text.Encoding.UTF8);
            mail.Subject = mailSubject.ToString();
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = mailDescription.ToString();
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = System.Net.Mail.MailPriority.High;

            System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();

            Client.Credentials = new System.Net.NetworkCredential(frmAddress.ToString(), srvMailPassword.ToString());
            Client.Port = Convert.ToInt32(port);
            Client.Host = smtpHost.ToString();
            Client.EnableSsl = true;

            Client.Send(mail);
            sent = true;

        }
        catch (Exception ex)
        {
            sent = false;
        }
        finally
        {

        }
        return sent;
    }




函数调用是




the function calling is

sendMail(Emaild, "softwarebuddy@gmail.com", "mypassword", "Rushi Tammisetti", "Password Recovery", "smtp.gmail.com", "Dear User Your Username Is is:" + username.ToString() + "\n Password is :" + pwd.ToString() + "", "587");




请建议我.出什么问题了??

最好的问候,
Rushi




please suggest me.what the wrong is.?

Best Regards,
Rushi

    bool SendMail (string strFromEMail, string strFromName, string strPass, string strToEMail, string strToName string strSub, string strBody)
{
            SmtpClient smtpClient = new SmtpClient( "smtp.gmail.com", 465 );
            System.Net.NetworkCredential credentials = null ;
             bool bSuccess = true ;
            
            MailMessage message = new MailMessage();
            // Try to send the message
            try
            {
                // Prepare two email addresses 
                MailAddress fromAddress = new MailAddress(strFromEMail, strFromName );
                MailAddress toAddress = new MailAddress (strToEMail, strToName);
                // Prepare the mail message
                message.From = fromAddress;
                message.To.Add(toAddress);
                message.Subject = strSub;
                message.IsBodyHtml = true;
                credentials = new System.Net.NetworkCredential( strFromEMail, strPass);
                smtpClient.Credentials = credentials;
                smtpClient.Send(message); 
            }
            catch ( Exception eX )
            {
                 // eX.Message shows  the cause of the error, if it fails. 
                 bSuccess = false ;
            }
            return bSuccess ;
}


Gmail 可以在端口587上运行,并在我的终端进行了测试.

遵循我以前的回答-从asp.net发送电子邮件到gmail [ ^ ].

如果仍然遇到问题,请检查InnerException以找到确切的问题.
Gmail works on Port 587 and it is tested at my end.

Follow my previous answer - sending email to gmail from asp.net[^].

If you still face the issue, then please check the InnerException to find the exact issue.