By Bill Cunnien - 6/5/2009
I am stumped...here is my code:
MailMessage mMail = new System.Net.Mail.MailMessage();
mMail.Body = String.Format("Sales order ID {0} has been recently edited by {1}. The new order total {2:c} has increased which exceeds the previously approved amount of {3:c}.", mSalesOrder.orderid, AspireGlobals.CurrentUser.FullName, mSalesOrder.OrderTotal, _salesordertotal);
mMail.From = new MailAddress(AspireGlobals.CurrentUser.EmailAddress);
mMail.Subject = "Sales Order Exceeds Approved Total!";
mMail.To.Add(new MailAddress("bcunnien@spiratex.com"));
SmtpClient mSmtpClient = new SmtpClient("192.168.2.247");
mSmtpClient.UseDefaultCredentials = true;
try
{
mSmtpClient.Send(mMail);
}
catch (Exception ex)
{
MessageForm.ShowMessage("Aspire Information", String.Format("{0} :: {1} ({2})", ex.Message, ex.InnerException, ex.StackTrace), "Send Email", MicroFour.StrataFrame.Messaging.MessageFunction.OK, MicroFour.StrataFrame.Messaging.MessagingIcon.Information, MicroFour.StrataFrame.Messaging.MessagingSounds.Notify);
}
I get no error. No email is sent. The IP address is our Exchange server. Is there some credential's trick that I need to get SMTP traffic to flow through our Exchange server? I have tried other servers that have SMTP running, but none of them are sending an email, either. I can watch a connection being established in the Client Connections of the SMTP server via IIS...the connection persists until I actually close my application or it times out.
Anyone have any ideas about this one?
Thanks!!
Bill
|
By Trent L. Taylor - 6/5/2009
Yeah, if you do not support relay (as you most likely do not) then you need to supply the credentials. For example:
SmtpClient _SMTP = new SmtpClient("MyMailServer", 25);
NetworkCredential _LoginCredentials = new NetworkCredential("UserName", "Password", "Domain");
_SMTP.Credentials = _LoginCredentials;
You may add this and see if you get anywhere. I will say, however, that you will generally get an error if it cannot authenticate. But this might at least prompt some ideas!
|
By Keith Chisarik - 6/5/2009
I know this works. MyEmail is just a structure that holds the email content and authentication info.Public Sub SendEmail(ByVal messageInfo As MyEmail)Dim mail_Email As New MailMessage()Dim smtp As New SmtpClient(messageInfo._smtpServer)mail_Email.To.Add(messageInfo._ToAddress) mail_Email.From = New MailAddress(messageInfo._FromAddress, messageInfo._FromText)mail_Email.Subject = messageInfo._Subject mail_Email.IsBodyHtml = Truemail_Email.Body = Err.ToString smtp.Credentials = New Net.NetworkCredential(messageInfo._smtpUser, messageInfo._smtpPassword)smtp.Send(mail_Email) mail_Email.Dispose() End Sub
|
By Bill Cunnien - 6/5/2009
Thanks! I did try the NetworkCredential's route as you guys outlined. Still no email gets sent. Still stumped.
|
By Edhy Rijo - 6/5/2009
Be aware that many antivirus program will block port 25 for SMTP and also may ISP may monitor the traffic used by the smtp and may be blocking out your IP or your SMTP server.
|
By Keith Chisarik - 6/5/2009
Bill, I PM'd you some code to test using my public SMTP server, I know the code works, if you cant send with that... it must be ISP, firewall, etc
|
By Bill Cunnien - 6/8/2009
Here's the thing guys (btw, thanks to everyone for the help...much appreciated!), the From address cannot be in the same domain.
Yup, I agree it is quite odd. Here is my adjusted code that actually works!
MailMessage mMail = new MailMessage();
mMail.IsBodyHtml = true;
mMail.Body = String.Format("Sales order ID {0} has been recently edited by {1}. The new order total {2:c} has increased which exceeds the previously approved amount of {3:c}.", mSalesOrder.orderid, AspireGlobals.CurrentUser.FullName, mSalesOrder.OrderTotal, _salesordertotal);
[highlight=#ffff11]mMail.From = new MailAddress("aspire@spiratex.net");[/highlight]
mMail.Subject = "Sales Order Exceeds Approved Total!";
mMail.To.Add("userA@spiratex.com");
mMail.CC.Add("userB@spiratex.com");
SmtpClient mSmtpClient = new SmtpClient("SPFS01");
try
{
mSmtpClient.Send(mMail);
}
catch (Exception ex)
{
MessageForm.ShowMessage("Aspire Information", String.Format("{0} :: {1} ({2})", ex.Message, ex.InnerException, ex.StackTrace), "Send Email", MicroFour.StrataFrame.Messaging.MessageFunction.OK, MicroFour.StrataFrame.Messaging.MessagingIcon.Information, MicroFour.StrataFrame.Messaging.MessagingSounds.Notify);
}
This is working so I can pragmatically move on. Does anyone know the reason for this behavior?
Thanks, again!
Bill
|
By Bill Cunnien - 6/8/2009
Sorry...apparently highlighting does not work within a code snippet. The key change was the following line:
mMail.From = new MailAddress("aspire@spiratex.net");
|
By Trent L. Taylor - 6/9/2009
That doesn't make any sense! This could be coming from your mail server more than from the SMTP client. I will have to test this and see if I get the same results...that is definitely strange! Glad you got it going!
|
By Bill Cunnien - 6/9/2009
On our Exchange server, one of the anti-spam policies was being run in the wrong order. I moved the Custom Allow by IP Address up before the Anti-Spoofing policy and now I can send intra-domain emails via code.
I am glad I finally got this solved.
Time for a vacation.
|
|