在asp.net中通过c#发送电子邮件

时间:2015-08-31 05:50:10

标签: c# smtp gmail

以下是我的邮件发送邮件的代码但我收到错误请帮忙!

  

错误:未发送电子邮件!System.Net.Mail.SmtpException:操作已超时。在System.Net.Mail.SmtpClient.Send(MailMessage消息)   在   _Default.Button1_Click(Object sender,EventArgs e)

try{

        MailMessage mailmessage = new MailMessage();
        mailmessage.To.Add(TextBox3.Text);
        mailmessage.From=new MailAddress("sadiazar05@gmail.com");
        mailmessage.Subject = "User SignUp";
        mailmessage.Body = "Hello You're registered!";
        SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",465);
        mailmessage.Priority = MailPriority.High;
        smtpclient.Timeout = 60000;
        smtpclient.Send(mailmessage);
        Response.Write("Email sent successfully!");
        }
        catch(Exception exp)
        {
            Response.Write("Email not sent!" +exp);
        }
    }

4 个答案:

答案 0 :(得分:0)

您可以尝试使用端口25,如果它不起作用,您可以尝试端口587:

SmtpClient smtpclient = new SmtpClient("smtp.gmail.com",The Other Port);

答案 1 :(得分:0)

尝试this code并启用SSL。

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);           
 client.EnableSsl = true;
 MailAddress from = new MailAddress("YourGmailUserName@gmail.com", "[ Your full name here]");           
 MailAddress to = new MailAddress("your recipient e-mail address", "Your recepient name");
 MailMessage message = new MailMessage(from, to);
 message.Body = "This is a test e-mail message sent using gmail as a relay server ";
 message.Subject = "Gmail test email with SSL and Credentials";
 NetworkCredential myCreds = new NetworkCredential("YourGmailUserName@gmail.com", "YourPassword", "");           
 client.Credentials = myCreds;
 client.Send(message);

答案 2 :(得分:0)

您可以尝试这样:

try{
    // message details
    MailMessage mailmessage = new MailMessage();
    mailmessage.To.Add(TextBox3.Text);
    mailmessage.From=new MailAddress("sadiazar05@gmail.com");
    mailmessage.Subject = "User SignUp";
    mailmessage.Body = "Hello You're registered!";      
    mailmessage.Priority = MailPriority.High;
    //smtp Client details
    smtpclient.UseDefaultCredentials = False
    smtpclient.Credentials = New Net.NetworkCredential("email", "password")// here you have to give your username and password
    smtpclient.Port = 587 // default port for gmail
    smtpclient.EnableSsl = True
    smtpclient.Host = "smtp.gmail.com"
    smtpclient.Timeout = 60000;
    smtpclient.Send(mailmessage);
    Response.Write("Email sent successfully!");
    }
    catch(Exception exp)
    {
        Response.Write("Email not sent!" +exp);
    }
}

答案 3 :(得分:0)

587是Gmail端口

smtpclient.Port = 587

默认情况下,SMTP使用TCP端口25.邮件提交的协议相同,但使用端口587.由SSL保护的SMTP连接(称为SMTPS)默认为端口465(非标准,但有时用于遗留原因)。