简体   繁体   English

使用C#发送邮件

[英]Sending mail using C#

I have to send mail using C#. 我必须使用C#发送邮件。 I follow each and every step properly, but I cannot send mail using the code below. 我正确地执行了每个步骤,但是无法使用下面的代码发送邮件。 Can anybody please help me to solve this issue? 有人可以帮我解决这个问题吗? I know it's an old issue and I read all the related articles on that site about it. 我知道这是一个老问题,我阅读了该网站上与此有关的所有相关文章。 But I cannot solve my issue. 但是我不能解决我的问题。 So please help me to solve this problem. 所以请帮我解决这个问题。 The error is: Failure sending mail . 错误是: Failure sending mail I use System.Net.Mail to do it. 我使用System.Net.Mail来做到这一点。

using System.Net.Mail;

string mailTo = emailTextBox.Text;
string messageFrom = "riad@abc.com";
string mailSubject = subjectTextBox.Text;
string messageBody = messageRichTextBox.Text;
string smtpAddress = "mail.abc.com";
int smtpPort = 25;
string accountName = "riad@abc.com";
string accountPassword = "123";

MailMessage message = new MailMessage(messageFrom, mailTo);                            
message.Subject = mailSubject;
message.SubjectEncoding = System.Text.Encoding.UTF8;
message.Body = messageBody;
message.BodyEncoding = System.Text.Encoding.UTF8;              
SmtpClient objSmtp = new SmtpClient(smtpAddress, smtpPort);
objSmtp.UseDefaultCredentials = false;
NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(accountName, accountPassword);
objSmtp.Credentials = basicAuthenticationInfo;
objSmtp.Send(message);                                
MessageBox.Show("Mail send properly");

If the target mail server is an IIS SMTP (or indeed any other) server then you'll have to check the relay restrictions on that server. 如果目标邮件服务器是IIS SMTP(或其他任何服务器),则必须检查该服务器上的中继限制。

Typically, you either have to configure the mail server to accept incoming relay from your machine's name (if in Active Directory) or IP address. 通常,您必须配置邮件服务器以接受来自计算机名称(如果在Active Directory中)或IP地址的传入中继。 Either that, or you can make it an Open Relay - but if it's a public mail server then that is not recommended as you'll have spammers relaying through it in no time. 要么这样做,要么可以将其设置为开放中继-但是,如果它是公共邮件服务器,则不建议这样做,因为垃圾邮件发送者会立即通过它进行中继。

You might also be able to configure the server to accept relayed messages from a particular identity - and if this is website code that'll mean that you will most likely have to configure the site to run as a domain user so that the NetworkCredentials are sent over correctly. 您也许还可以将服务器配置为接受来自特定身份的中继消息-如果这是网站代码,则意味着您很可能必须将站点配置为以域用户身份运行,以便发送NetworkCredentials正确结束。

oh friends...i got the solution. 哦,朋友...我找到了解决方案。

just i used the port 26.now the mail is sending properly. 只是我使用了端口26.now邮件正在正确发送。

int smtpPort = 26;

anyway thanks to Zoltan 无论如何要感谢佐丹

riad. 里亚德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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