How to send email in C #

zhaozj2021-02-17  86

In C #, we can usually use MailMessage under System.Web.mail to send email:

Using system.web.mail;

Public void sendmail ()

{

MailMessage msg = new mailmessage ();

Msg.to = "xxx@xxx.com";

Msg.subject = "hello";

Msg.body = "testing email";

Msg.from = "xxx@xxx.com";

SMTPMAIL.SMTPSERVER = "smtp.netese.com"; // << === pay attention to this address

SMTPMAIL.SEND (MSG);

Return;

}

But in fact, the above code is not working properly because SMTP.NETEASE.com requires user authentication, and I first I found that SMTPMail can only use the SMTP server that allows anonymous users (think about SMTP servers that support anonymous access? They are all damn spam hurts :-)). I checked the information and found that in C # we still need to complete the user authentication by calling COM (CDO for Windows 2000).

Here is the test code, the test passes: vs.net win2k prof.

// Add cdo for windows 2000 in Reference

Using CDO;

Public void sendemail () {

try {Configuration conf = new ConfigurationClass (); conf.Fields [CdoConfiguration.cdoSendUsingMethod] .Value = CdoSendUsing.cdoSendUsingPort; conf.Fields [CdoConfiguration.cdoSMTPServer] .Value = "smtp.netease.com"; conf.Fields [CdoConfiguration. cdoSMTPServerPort] .Value = 25; conf.Fields [CdoConfiguration.cdoSMTPAccountName] .Value = "hydnoahark"; conf.Fields [CdoConfiguration.cdoSendUserReplyEmailAddress] .Value = "/" hydnoahark / ""; conf. Fields [CdoConfiguration.cdoSendEmailAddress] .Value = "/" hydnoahark / ""; conf.Fields [CdoConfiguration.cdoSMTPAuthenticate] .Value = CdoProtocolsAuthentication.cdoBasic; conf.Fields [CdoConfiguration.cdoSendUserName] .Value = "Hydnoahark"; conf.fields [cdoconfiguration.cdosendpassword] .value = "xxx"; conf.fields.Update ();

MessageClass msg = new messageclass (); msg.configuration = conf; msg.to = "hydnoahsark@sina.com"; msg.subject = "hello"; msg.textbody = "it's test"; msg.from = "HydNoahark @ NETEASE.COM ";

Msg.send ();} catch (system.runtime.interopservices.com ).comException e) {messagebox.show (e.tostring ());}

Return;}

OK, it is so simple, I don't know if someone else has a better way, I can give me a suggestion ^ _ ^

转载请注明原文地址:https://www.9cbs.com/read-31197.html

New Post(0)