如何通过C#asp.net发送邮件联系表格

时间:2022-08-12 18:14:02

I am trying to create contact form to send email (from and to will be from user interface):

我正在尝试创建联系表单以发送电子邮件(来自和将来自用户界面):

try {
   MailMessage mail = new MailMessage();
   SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
   mail.From = new MailAddress("fromadd");
   mail.To.Add("toadd");
   mail.Subject = "Test Mail";
   mail.Body = "This is for testing SMTP mail from GMAIL";
   SmtpServer.Port = 587;
   SmtpServer.Credentials = new System.Net.NetworkCredential("username","password");
   SmtpServer.EnableSsl = true;

   SmtpServer.Send(mail);
   MessageBox.Show("mail Send");
}
catch (Exception ex) {
   MessageBox.Show(ex.ToString());
}

This works for only Gmail - however, I would like to make it work for any email provider - how would I go about this?

这仅适用于Gmail - 不过,我想让它适用于任何电子邮件提供商 - 我该如何处理?

6 个解决方案

#1


2  

You should configure the SmtpClient in the web.config:

您应该在web.config中配置SmtpClient:

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network
          host="localhost"
          port="25"
          defaultCredentials="true"
        />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Then in your code you can do:

然后在您的代码中,您可以:

    try
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("fromadd");
        mail.To.Add("toadd");
        mail.Subject = "Test Mail";
        mail.Body = "This is for testing SMTP mail from GMAIL";

        SmtpClient SmtpServer = new SmtpClient();            
        SmtpServer.Send(mail);
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

#2


1  

You can set up your SmtpClient configuration in your web.config. This will make it flexible.

您可以在web.config中设置SmtpClient配置。这将使其灵活。

http://blog.dotnetclr.com/archive/2009/08/18/511.aspx

http://blog.dotnetclr.com/archive/2009/08/18/511.aspx

http://msdn.microsoft.com/en-us/library/w355a94k.aspx

http://msdn.microsoft.com/en-us/library/w355a94k.aspx

#3


1  

Don't use hardcoded parameters for the connection to the smtp-server.

不要使用硬编码参数连接到smtp-server。

Use the webconfig instead. Your program will be more "generic". Just alter the config when you want to send through another smtp-server

请改用webconfig。你的程序将更“通用”。当您想通过另一个smtp-server发送时,只需更改配置即可

#4


1  

You can also try:

你也可以尝试:

    MailMessage msgObj = new MailMessage();

    msgObj.To = "example@example.com";
    msgObj.From = "Mike";
    msgObj.Bcc = "example@example.com";
    msgObj.Subject = "Test Message";
    msgObj.Body = "Hello World!";
    SmtpMail.SmtpServer = "Your Server";
    SmtpMail.Send("FromEmail", "ToEmail", "Subject", "Query");
    SmtpMail.Send(msgObj);

#5


0  

To me it looks like the only thing that might be preventing you from sending the email using any email server is the fact that some mail servers require authentication (or possibly an alternative port number).

对我来说,似乎唯一可能阻止您使用任何电子邮件服务器发送电子邮件的事实是某些邮件服务器需要身份验证(或可能是替代端口号)。

Here is a bit of basic code that should get you pointed in the right direction

这里有一些基本的代码可以让你指向正确的方向

public class SendMail
{

    public SendMail(string SMTPServer, string fromEmail)
    {
        this.SMTPServer = SMTPServer;
        this.FromEmail = fromEmail;
    }

    public SendMail(string SMTPServer, string fromEmail, string Username, string Password) : this(SMTPServer, fromEmail)
    {
        this.Username = Username;
        this.Password = Password;
    }

    public string SMTPServer { get; set; }
    public string FromEmail { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public void Send(string toEmail, string subject, string data)
    {
        MailMessage mailMsg = new MailMessage();
        mailMsg.To.Add(toEmail);

        MailAddress mailAddress = new MailAddress(this.FromEmail);

        mailMsg.From = mailAddress;

        mailMsg.Subject = subject;
        mailMsg.Body = data;
        mailMsg.IsBodyHtml = true;
        SmtpClient smtpClient = new SmtpClient(this.SMTPServer, 25);

        if (this.Username.Length > 0 && this.Password.Length > 0)
        {
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(this.Username, this.Password);
            smtpClient.Credentials = credentials;
        }

        smtpClient.Send(mailMsg);
    }

}

#6


0  

i am trying this code .

我正在尝试这段代码。

try
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("best@technosys.com");
    mail.To.Add("best@technosys.com");
    mail.Subject = "Accept Request";
    mail.Body = body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
   smtp.Credentials = new System.Net.NetworkCredential("best@technosys.com", " password");
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = true;
    smtp.Send(mail);
}
catch (Exception ex)
{
   ViewData.ModelState.AddModelError("_FORM", ex.ToString());
}

#1


2  

You should configure the SmtpClient in the web.config:

您应该在web.config中配置SmtpClient:

<configuration>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="network">
        <network
          host="localhost"
          port="25"
          defaultCredentials="true"
        />
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

Then in your code you can do:

然后在您的代码中,您可以:

    try
    {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress("fromadd");
        mail.To.Add("toadd");
        mail.Subject = "Test Mail";
        mail.Body = "This is for testing SMTP mail from GMAIL";

        SmtpClient SmtpServer = new SmtpClient();            
        SmtpServer.Send(mail);
        MessageBox.Show("mail Send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }

#2


1  

You can set up your SmtpClient configuration in your web.config. This will make it flexible.

您可以在web.config中设置SmtpClient配置。这将使其灵活。

http://blog.dotnetclr.com/archive/2009/08/18/511.aspx

http://blog.dotnetclr.com/archive/2009/08/18/511.aspx

http://msdn.microsoft.com/en-us/library/w355a94k.aspx

http://msdn.microsoft.com/en-us/library/w355a94k.aspx

#3


1  

Don't use hardcoded parameters for the connection to the smtp-server.

不要使用硬编码参数连接到smtp-server。

Use the webconfig instead. Your program will be more "generic". Just alter the config when you want to send through another smtp-server

请改用webconfig。你的程序将更“通用”。当您想通过另一个smtp-server发送时,只需更改配置即可

#4


1  

You can also try:

你也可以尝试:

    MailMessage msgObj = new MailMessage();

    msgObj.To = "example@example.com";
    msgObj.From = "Mike";
    msgObj.Bcc = "example@example.com";
    msgObj.Subject = "Test Message";
    msgObj.Body = "Hello World!";
    SmtpMail.SmtpServer = "Your Server";
    SmtpMail.Send("FromEmail", "ToEmail", "Subject", "Query");
    SmtpMail.Send(msgObj);

#5


0  

To me it looks like the only thing that might be preventing you from sending the email using any email server is the fact that some mail servers require authentication (or possibly an alternative port number).

对我来说,似乎唯一可能阻止您使用任何电子邮件服务器发送电子邮件的事实是某些邮件服务器需要身份验证(或可能是替代端口号)。

Here is a bit of basic code that should get you pointed in the right direction

这里有一些基本的代码可以让你指向正确的方向

public class SendMail
{

    public SendMail(string SMTPServer, string fromEmail)
    {
        this.SMTPServer = SMTPServer;
        this.FromEmail = fromEmail;
    }

    public SendMail(string SMTPServer, string fromEmail, string Username, string Password) : this(SMTPServer, fromEmail)
    {
        this.Username = Username;
        this.Password = Password;
    }

    public string SMTPServer { get; set; }
    public string FromEmail { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public void Send(string toEmail, string subject, string data)
    {
        MailMessage mailMsg = new MailMessage();
        mailMsg.To.Add(toEmail);

        MailAddress mailAddress = new MailAddress(this.FromEmail);

        mailMsg.From = mailAddress;

        mailMsg.Subject = subject;
        mailMsg.Body = data;
        mailMsg.IsBodyHtml = true;
        SmtpClient smtpClient = new SmtpClient(this.SMTPServer, 25);

        if (this.Username.Length > 0 && this.Password.Length > 0)
        {
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(this.Username, this.Password);
            smtpClient.Credentials = credentials;
        }

        smtpClient.Send(mailMsg);
    }

}

#6


0  

i am trying this code .

我正在尝试这段代码。

try
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress("best@technosys.com");
    mail.To.Add("best@technosys.com");
    mail.Subject = "Accept Request";
    mail.Body = body;
    mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
   smtp.Credentials = new System.Net.NetworkCredential("best@technosys.com", " password");
    smtp.EnableSsl = true;
    smtp.UseDefaultCredentials = true;
    smtp.Send(mail);
}
catch (Exception ex)
{
   ViewData.ModelState.AddModelError("_FORM", ex.ToString());
}