开发者

Sending mail using C#

I have to send mail using 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. I use System.Net.Mail to do it.

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 =开发者_StackOverflow中文版 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.

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. 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.


oh friends...i got the solution.

just i used the port 26.now the mail is sending properly.

int smtpPort = 26;

anyway thanks to Zoltan

riad.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜