Asp.net 4.0发送电子邮件

时间:2013-06-17 03:43:26

标签: asp.net

我有aspx页面,我有这样的电子邮件字段

<input class="span12" type="text" placeholder="EMAIL" id="Email" name="Email" runat="server" />

在我的Csharp文件中,我有代码并使用请求[“电子邮件”]来获取访问者输入电子邮件地址的地址,这可以是任何所以我想通过电子邮件发送给他们以及我的代码就像下面,但它没有工作,我正在使用.net 4.0,我可以在那里更改动态电子邮件,无论它是什么,我都可以得到它并发送电子邮件。

private void SendEmail(int RefNum)
{
    var customerEmail = Request["Email"]; //getting value from aspx page.
    MailMessage ObjEmail = new MailMessage();
    ObjEmail.SendFrom = "carlaza@hotmail.ca";
    ObjEmail.SendTo = "zjaffary@hotmail.com";
    ObjEmail.SendCC = "jaffary_zafar@hotmail.com";
    ObjEmail.SendBCC =  customerEmail ;
    ObjEmail.Subject = "test Subject ";
    //Development
    //SmtpMail.SmtpServer = "tormail.corp.kkt.ca";
    //Production At Bell
    SmtpMail.SmtpServer = "tormail.corp.kkt.ca";

    ObjEmail.BodyFormat = MailFormat.Html;

    string strBody1 = "Test message " ;
    ObjEmail.Priority = MailPriority.High;

try {
    SmtpMail.Send(ObjEmail);
    lblResponse.Text = "Thank you for sending the form !";
    Response.AddHeader("Refresh", "2;URL=index.aspx");

    }

    catch (Exception exc){
    Response.Write("Send failure: " + exc.ToString());
    }

}

3 个答案:

答案 0 :(得分:0)

您应该使用来自Web邮件服务器的身份验证信息。 (用户名和密码)如果不是,那不是实际的电子邮件。

答案 1 :(得分:0)

您可以看到代码并且可以正常工作

SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("youremail@hotmail.com");
mail.To.Add("to@gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("youremail@hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

请参阅主题。我认为这对你有帮助。 How to add smtp hotmail account to send mail How to add smtp hotmail account to send mail

答案 2 :(得分:0)

试试这个

    protected void sendEmail(string subject, string ToEmail, string msg)
    {
        String body = msg;
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        MailAddress fromAddress = new MailAddress("your_email_id");
        smtpClient.Host = "smtp.gmail.com";//host
        smtpClient.Port = 587;//port no. default 25
        smtpClient.UseDefaultCredentials = false;
        smtpClient.EnableSsl = true;
        smtpClient.Credentials = new System.Net.NetworkCredential("your_email_id", "password");
        message.From = fromAddress;
        message.To.Add(ToEmail);//if more than comma seprated
        message.Subject = subject;
        message.Priority = MailPriority.High;
        message.Body = body;
        message.IsBodyHtml = true;
        smtpClient.Send(message);
    }