SMTP: cannot send email from windows server 2012 - c#

I have strictly followed this tutorial in order to configure SMTP server on Windows Server 2012 R2 by usng IIS 6. In fact, if I create a correct formatted file email.txt inside
C:\inetpub\mailroot\Pickup
with From, To and Subject fields, it is dispatched immediately to the destination email address with the correct sender.
Then, I have a framework generating my ASP NET C# web application ad when it tried to send an email with:
IDMailer v_M1 = null;
v_M1 = (IDMailer)new IDMailer();
v_M1.FromAddress = (new IDVariant("allegretti#er-go.it")).stringValue();
v_M1.AddToAddress((new IDVariant("lorenzo.vinci#conservice.it")).stringValue());
v_M1.Subject = (new IDVariant("prova")).stringValue();
v_M1.HTMLBody = (new IDVariant("<b>ciao!</b>")).stringValue();
try
{
v_M1.SendMail();
}
catch (Exception e3)
{
MainFrm.set_AlertMessage(new IDVariant(e3.Message));
}
then I have an error in sending the email which fails. I have tried also both the following settings in the application pool of my web application:
and they both fail with these error messages:
Thursday, October 6, 2016 - 5:38:09 PM
Failure sending mail.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at com.progamma.IDMailer.SendMail()
DTT_LEVEL=5
DTT_MSGLEVEL=3
DTT_LEVEL=5
DTT_MSGLEVEL=3
Failure sending mail.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at com.progamma.IDMailer.SendMail()
The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available
at System.Web.Mail.SmtpMail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)
at System.Web.Mail.SmtpMail.CdoSysHelper.Send(MailMessage message)
at System.Web.Mail.SmtpMail.Send(MailMessage message)
at com.progamma.IDMailer.SendMailSSL()
--------------------------------------------------------------------
Thursday, October 6, 2016 - 5:38:25 PM
Failure sending mail.
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at com.progamma.IDMailer.SendMail()
I have no idea why this happens but I really need to correctly set up a SMTP server in our Windows Server. I had tried to use Google smtp server as realy and it was ok but it did not allow us to set an arbitrary sender email address but only the one of the account used to connect to gmail smtp server. That's why I need my SMTP server because sender anddress will vary according to the user logging into the web application.
How can I solve this?
Maybe does this happen because the web applicaion is installed inside drive D (which has its own pickup emailroot directories) instead of C drive?

Related

SMTP works in Visual Studio but not when uploading to production IIS Server

I have narrowed this down to an issue with IIS. I am using system.net.mail to send emails. They work on my local box, but when I publish them to my IIS8 Server 2012 Server, the emails fail to send.
Try
Dim mailMessage As New MailMessage("test#test.com", Me.ddlApprovers.SelectedValue)
mailMessage.Subject = "TEST"
mailMessage.Body = "TEST"
Dim smtpClient As New SmtpClient(My.Settings.SMTP, 25)
smtpClient.Send(mailMessage)
Catch ex As Exception
Me.lblAlertText.Text = "An error send email has occured. Contact your system administrator."
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "LaunchAlertPopup", "$(function() { AlertMessage(); });", True)
End Try
I have downloaded an SMTP test tool and send emails just fine through it, so it has to be an issue with this website in IIS.
Many ISPs block port 25. Try sending through 465 or 587. It would also be worth checking for any firewall rules that could be blocking any of these ports.
We can only assume that you're using a valid host name for your SMTP client. This needs to exist or your messages will never send.

Exchange 2003 with C# smtpclient

I am currently trying to use SmtpClient to send an email with exchange 2003. Using the code below it will not send an email, but it does not throw an exception which i assume means it is making a connection to the exchange server because. Also for the server setting I tried mail.server.com as well as the IP address and it still does not throw an exception.
public static void emailTest()
{
string fromEmail = #"me#me.com";
string ToEmail = #"me#me.com";
string body = "C Stuck Batches";
string subject = "C Stuck Batches";
try
{
SmtpClient MyMail = new SmtpClient("x.x.x.x");
MyMail.DeliveryMethod = SmtpDeliveryMethod.Network;
MyMail.UseDefaultCredentials = false;
MyMail.Credentials = new NetworkCredential(#"domain\user", "password");
MyMail.Send(fromEmail, ToEmail, subject, body);
MessageBox.Show("Sent", "SENT", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch (Exception ex)
{
MessageBox.Show("Exception", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
I have read through several other problems, but as far as I could tell most others were getting some sort of exception or timeout. Since this appears as though it connects I am not sure what the issue is. Would it throw an exception if it were unable to access the server? If I throw a different IP into the server it will throw an exception.
Also I have tried both the IP of the server and mail.xx.com and it will not throw an exception with either but if I put any other addresses it will fail.
Thanks in advance for anyhelp!
Is your Exchange server set up to relay mail from your IP address? If it isn't, mail may be "silently" discarded without throwing an Exception (the client is connecting correctly, it's the back end that's discarding the email)
This link provides more information on how to configure relaying on Exchange 2003 (I'm assuming "eSupport" would equate to your application in this example i.e. the IP of the machine running your code).
Log into Exchange 2003 server (192.168.5.25 in my example above) and start Exchange System Manager.
Expand the organization_name object, and then expand the Servers node. Expand the server_name object of the server on which you want to control mail relay, and then expand the Protocols node.
Expand the SMTP node, right-click the virtual SMTP server on which you want to control mail relay, and then click Properties
Click Relay.
In the Relay Restriction dialog box, several options are available. The Only the list below option is enabled by default; the list below this option is empty.
Click Add. Enter the IP address of the server running eSupport (192.168.5.80 in my example above).
In the Relay Restrictions dialog box, click OK.
Click Apply, and then click OK in the Default SMTP Virtual Server Properties dialog box.
The issues i had was that even the domain admins did not have access to the SMTP relay. I had to make sure the user was authenticated in the SMTP relay. Regardless of the fact the authenicating user was the same as the email adress used, that user did not have access. Once corrected it was able to send without issue.

SMTP : error relaying

i am creating an email client that sends e-mail address from server ip instead of SMTP , i wrote that code:
SmtpClient server = new SmtpClient("50.23.128.66");
MailMessage msg = new MailMessage("from#yahoo.com", "tome#yahoo.com", "subject", "body");
server.Send(msg);
but when i run it , i get that error:
Unhandled Exception: System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Relaying Denied.
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressColl
ection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
* By the way i am using Windows Server 2008 *
and i configured the smtp server to localhost and port 25 , but i don`t know what is that relying error .
If you are using you localhost to rely email, you should be using 127.0.0.1 or localhost instead of that ip address you posted in your code.
Relaying is the method the SMTP server uses to authenticate that it should route the email from a particular sender. An "open relay" means that there is no authentication and the SMTP server will send email sent from anyone. This is not a good practice and there are probably some mechanisms on this particular SMTP server to authenticates, such as a user name and password or even the IP address of the sender. Check with the administrator of the SMTP server to see what is required. I would think at a minimum you would need to set the user name and password, which you did not do in your code example.

How to send email through IIS7?

I'm trying to set up a SMTP server on my Windows 7 machine in IIS7. I have set it to "Deliver email to localhost, port 25, no authentication. But when I try to connect programmatically from my C# program, I get an error:
Failure sending mail", inner exception "No connection could be made because the target machine actively refused it 127.0.0.1:25
public static void SendEmail(MailMessage m) {
var smtp = new SmtpClient {
Host = "localhost",
Port = 25,
UseDefaultCredentials = true,
};
smtp.Send(m);
}
Why? What other secret switch do I have to flip?
For development purposes I use storing mails to filesystem, try this in the web.config
<mailSettings>
<smtp deliveryMethod="SpecifiedPickupDirectory">
<specifiedPickupDirectory pickupDirectoryLocation="c:\temp\mails\" />
</smtp>
</mailSettings>
Sorry, but most of these answers were totally confusing and don't explain the problem . Here is what the problem is.
The "SMTP E-mail" in IIS 7, on both Windows 7 Professional and "real" web server like Windows Server 2008 is that SMTP E-mail is not a true "virtual SMTP server" or what Microsoft calls "Simple Mail Transfer Protocol (SMTP)". Its just a an interface that allows you to apps that to an SMTP server online. The virtual SMTP server we used to have on older Windows is now only available as an add-on on server operating systems using the "Server Manager" under Administrative Tools and clicking "add features". Thats not found on say Windows 7 Professional. Yet another Microsoft blunder!
However, you can still use the "SMTP E-mail" piece under IIs in development or even your Web Server to route mail out to a real SMTP server. Its not like the old days when both were one and the same and you could route email out and back to your local box, etc for testing. They let you store that locally but thats not much help to me. Thats why when in SMTP E-mail "localhost" doesn't work. Thats what most people are saying. For that you would need to install third party software. A better solution is to just get the Windows Server Admin pack which has the virtual server and all the web server goodies found on the server OS and install that: http://www.sysprobs.com/install-admin-pack-windows-7-remote-desktop-manager
Keep in mind, you don't have to run ANY real SMTP virtual server on either your local box or on the server as long as you have an address to a real SMTP service (like "mail.yourwebhost.com"). Under IIS7, just click your SMTP E-mail piece under IIS7 and type in the address. But "localhost" wont wor'k. Usng SMTP E-mail with a remote host, I've found most SMTP or email providers require two additional things: a port other than "25", and you adding custom authentication credentials found under SMTP E-mail. Network Solutions likes to use your email address for the login and your address password. I hooked all that up and the mail c# object sent out mail without a virtual SMTP server on my local box. Last trick is be sure to also add this to your c# code:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(
"mail.yourhost.com",155);//add custom port here
//This object stores the authentication values
System.Net.NetworkCredential mycredentials = new System.Net.NetworkCredential(
"yourname#yourdomain.com", "passwordhere");
client.UseDefaultCredentials = false;
client.Credentials = mycredentials;
Best method I found was to use SMTP4Dev, which listens for emails, and shows you what was "sent", but doesn't actually send anything. Great for testing!
You need to setup SMTP server in IIS7, here are the instructions how to setup:
http://learn.iis.net/page.aspx/751/configure-smtp-e-mail-in-iis-7/

what is means of this problem when sending email through web-application

i have this error when i sending email through our web application
" Mailbox unavailable. The server response was: Requested action not taken: mailbox unavailable or not local"
this is detail of error
System.Net.Mail.SmtpFailedRecipientException was caught
Message=Mailbox unavailable. The server response was: Requested action not taken: mailbox unavailable or not local
Source=System
FailedRecipient=<email#email.com>
StackTrace:
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at email.Globals.SendMail(String EmailID, String subject, String message, String senderMail) in C:location
InnerException:
I would assume that your sender credentials are not in order - if the recipient's mailbox is unavailable, you will get an E-Mail saying so but the actual sending process should work out (if it's not a local recipient).
If I had to guess, I'd say you are trying to send an E-Mail with a sender address belonging to a domain that is not managed by the SMTP server you are using to send the E-Mail (e.g. a GMail address).
It means that the mailbox belonging to "email#email.com" is either not available (it is over its quota or something similar) OR the server is not allowing you to relay emails (which it would have to do if "email#email.com" is not a local domain). It is common for mail servers to behave this way, because it is not desirable for a mail server to act as an open relay.
My best guess is that you problem has to do with the latter. If you have control over the server, you could allow relaying from the server that's hosting your web application.

Categories

Resources