开发者

configuration of asp.net mailmessage

Can someone help me pls. I am ready to deploy my web application. I have a contact form that i want users to send me a message. Ive created the smtp when i click on submit, 开发者_JAVA百科i get the error message cannot be sent. The application is still on my local machine maybe thats why. But i just want to know if this code is good for my form:

Imports System.Net.Mail


Partial Class contact
    Inherits System.Web.UI.Page

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        If txtComments.Text.Length > 300 Then
            args.IsValid = False
        Else
            args.IsValid = True
        End If
    End Sub

    Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
        SendMail(txtEmail.Text, txtComments.Text)
    End Sub

    Private Sub SendMail(ByVal from As String, ByVal body As String)
        Dim mailServerName As String = "SMTP.relay-hosting.secureserver.net"
        Dim message As MailMessage = New MailMessage(from, "collins@collinsegbe.com", "feedback", body)
        Dim mailClient As SmtpClient = New SmtpClient

        mailClient.Host = mailServerName
        mailClient.Send(message)
        message.Dispose()
    End Sub
End Class

The error is indicated on this line of code: mailClient.Send(message) I will appreciate help from anyone


protected void Button9_Click(object sender, EventArgs e)
{
    {
        var fromAddress = new MailAddress("rusty109.stud@gmail.com");
        var toAddress = new MailAddress(TextBox13.Text);
        const string fromPassword = "commando1";
        string subject = TextBox14.Text;
        string body = TextBox12.Text;

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
    }
}

my old code when i wanted to email, you can use the gmail account if you wish, nothing in it... was set up to use this code :) using System.Net.Mail; using System.Net;


You need to add the username and password for the SMTP server by setting the SmtpClient's Credential property to an instance of the NetworkCredential class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜