如何在asp.net中从我的网站域发送电子邮件

时间:2015-01-13 16:38:08

标签: asp.net

我想从我的网站域名(www.sample.com)发送邮件 我写了下面的代码。它没有返回任何错误。但是,一旦我在服务器中上传页面,它就不会发送邮件。一旦点击电子邮件发送,它没有收到任何错误,但没有收到邮件。

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient("mail.sample.co.uk", 25);

smtpClient.Credentials = new System.Net.NetworkCredential("smtpuser@sample.co.uk", "pass@123");
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = false;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

//Setting From , To and CC
mail.From = new System.Net.Mail.MailAddress("smtpuser@sample.co.uk", "MyWeb Site");
mail.To.Add(new System.Net.Mail.MailAddress("myemail@gmail.com"));
mail.CC.Add(new System.Net.Mail.MailAddress("myemail1@gmail.com"));

smtpClient.Send(mail);

调用发送方法后,我在页面中收到“mailsend”响应文本。

请帮忙 长香

1 个答案:

答案 0 :(得分:0)

以下代码对我来说非常适合。确保您已在smtp服务器上添加了一个邮件帐户并将其激活。

private static string EMAIL_SERVER = "mailservername.mydomain.com";
    private static string EMAIL_NAME = "myMailAccount@mydomain.com";
    private static string EMAIL_PASSWORD = "complex_password123";
    private static string EMAIL_SEND_TO = "sample@mail.com";
    protected string SendMail()
    {

        try
        {

            System.Net.Mail.MailMessage objMM = new System.Net.Mail.MailMessage();
            objMM.From = new MailAddress("fromMe@mail.com", "john doe");
            objMM.To.Add(new MailAddress(EMAIL_SEND_TO));    //Note: this To a collection
            objMM.Subject = "Subject1";
            objMM.Body = "Hello world this is my text";
            objMM.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(EMAIL_SERVER);
            smtp.Credentials = new NetworkCredential(EMAIL_NAME, EMAIL_PASSWORD);
            smtp.Send(objMM);

        }
        catch (Exception e)
        {

            return "Message can not be send couse of error: " + e.ToString();
        }


        return "Message is send.";

    }