How Can i Send Mail Through Exchange Server by using SMTP

a 夏天 提交于 2019-12-04 11:20:53
Bala Kumar

I have done it. For more details about my code use this link.

Below Code is worked Fine with

Server : Windows Server 2003,Windows Server 2008,Windows Server 2008 R2

IIS : 6.0, 7.0

.Net Frame Wotk : 2.0,3.5,4.0

string sMessage;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{

 //you can provide invalid from address. but to address Should be valil
MailAddress fromAddress = new MailAddress("bala@technospine.com", "BALA");

smtpClient.Host = "Exchange Server Name";
smtpClient.Port = 25;
//smtpClient.Port = 587;


smtpClient.UseDefaultCredentials = true; 

message.From = fromAddress;
message.To.Add(bala@technospine.com); //Recipent email 
message.Subject = _subject;
message.Body = _details;
message.IsBodyHtml = true;

//smtpClient.EnableSsl = true; 

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

smtpClient.Send(message); 

sMessage = "Email sent.";
}
catch (Exception ex)
{
sMessage = "Coudn't send the message!\n " + ex.Message;
}


lblMailStatus.Text = sMessage;

You are attempting to send a mail message using Exchange. In order to do that, the sender (or sending process) must have permissions on the account it is logged in under to send on behalf of the user you are specifying as the sender. This is different from going through Exchange's SMTP mail transfer agent (MTA) in order to have Exchange receive and route an email message. So you are on the right track with knowing you should do this using SMTP, but you are just trying to use the wrong API for accomplishing this. You want to take a look at CDOSYS for sending it through the SMTP MTA without having to do user authentication. Search on System.Web.Mail.MailMessage for more specific examples - there are plenty out there. If the Exchange server does not seem to accept/deliver the SMTP message delivered to it in this fashion, you might simply need to open up its configuration a bit. In that event, the Exchange server is probably configured with tight security on routing of mail received via its SMTP MTA and just needs to have the IP address of the machine(s) you are sending these messages from configured to allow for mail forwarding.

try NetworkCredential nc = new Net.NetworkCredential("USERNAME", "PASSWORD","DOMAIN")

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!