简体   繁体   中英

C# - Send e-mail without having to login to server

I have an application that needs to send e-mails. Currently, this is what I am using:

        System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage();
        MyMailMessage.From = new System.Net.Mail.MailAddress(fromemail.Text);
        MyMailMessage.To.Add(toemail.Text);
        MyMailMessage.Subject = subject.Text;
        MyMailMessage.Body = body.Text;
        System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
        SMTPServer.Port = 587;
        SMTPServer.Credentials = new System.Net.NetworkCredential("email", "password");
        SMTPServer.EnableSsl = true;
        SMTPServer.Send(MyMailMessage);

Is there a simple way to send an e-mail without having to login to a server? Thank you.

GMail's SMTP server always requires authentication. You may need to setup your own server to send email without authentication.

Configure an SMTP server into your local network (behind a firewall to avoid being a spam source) and use it directly. You can create one in IIS.

There are 2 ways to achieve this:

1) Use your local smtp server (eg one with IIS on Win2003/2008 server) and write messages to the local pickup queue). This is possible with minimal changes.

2) You need to resolve the target smtp server. For example when you want to send an email to somebody at msn.com, you'll need to get the MX record for msn.com, eg something like mx1.msn.com. You can then directly connect to this SMTP server and send your email to the (local) recipient. Note that there are no built-in ways to resolve the MX-host in .NET (in the sense there are no methods on the Dns class to accomplish this) - you need to do it "manually". Also most SMTP hosts will reject connections from home/residential IP addresses.

您需要不需要身份验证的SMTP服务器,但是要使其停止作为SPAM服务器,则需要其他某种保护,例如防火墙。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM