Code Conversion from Classic ASP to ASP.NET - c#

My client have a classic ASP site on his dedicated server on which he sends mail using IIS (Its working properly there).
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "localhost" ' Specify a valid SMTP server
Mail.Username = "mail#site.com"
Mail.Password = "password"
Mail.From = "info#site.com"
I tried converting this to ASP.NET like this.
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Port = 25;
//smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mail#site.com", "password");
smtp.Send(message);
But this doesn't work. Its almost legacy app without any error logger / monitoring and I cannot debug the code on online server.
What is wrong with my code?

This seems like a permissions issue. Check this thread for details.

You might want to check to see if port 25 is being blocked, as shown in http://kb.siteground.com/article/How_to_check_whether_SMTP_port_25_is_blocked.html

Make sure the pickup directory is being monitored by your local SMTP server if you use this method:
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
If it is not then the messages will remain there without being sent.

Related

Unable to send Email through GoDaddy SMTP

I am working on an ASP .Net MVC website and I've to send email through Godaddy smtp, Previously my site was developed in classic ASP and it was hosted on godaddy's web hosting (then it was working fine) but now I am hosting this site on IIS,
I am using following code to send email but it is not working
MailMessage msg = new MailMessage();
msg.From = new MailAddress(model.From);
msg.To.Add(model.To);
msg.Body = model.Body;
msg.Subject = model.Subject;
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net",25);
smtp.Credentials = new NetworkCredentials("support#{myCompanyName}.com",{password});
smtp.EnableSsl = False;
smtp.Send(msg);
I have also used dedrelay.secureserver.net instead of relay-hosting.secureserver.net host (as mentioned at https://pk.godaddy.com/help/what-is-my-servers-email-relay-server-16601) but both are not working
GoDaddy does not allow relaying through their server unless you are on one of their hosting plans that includes SMTP.
You can set your credentials in webconfig like (for godaddy)
<system.net>
<mailSettings>
<smtp from="your email address">
<network host="relay-hosting.secureserver.net" port="25" />
</smtp>
</mailSettings>
</system.net>
and in c# you can use like
MailMessage message = new MailMessage();
message.From = new MailAddress("your email address");
message.To.Add(new MailAddress("your recipient"));
message.Subject = "your subject";
message.Body = "content of your email";
SmtpClient client = new SmtpClient();
client.Send(message);
It will work
Most SMTP servers are quite restrictive nowadays when it comes to outbound email. I recommend testing the parameters with an email client (or telnet, if you're into that kinda thing) before assuming that there is something wrong with the code. That might also give you an error message that helps debugging.
Some things that come to mind:
The server may check the FROM address against it's database, specifically the user account you use to authenticate. While you can put whatever you want in the header of the email, this field must be your the real address of the authenticated account and only that (no descriptive name).
The server may require the use of TLS encryption, regardless of the port.
Port 25 is quite common, but according to the official RFC mail submission should use port 587. Maybe try that.
It is possible that GoDaddy only allows connections from their own (hosting) servers to these SMTP relays.
Unless the connection fails completely (which would point to no. 4) the server should send some kind of error message at some point. As I wrote above, I would recommend testing/logging the communication, that should provide some insight.
Perhaps you should call up to your ISP, here in th Netherlands they mostly block port 25 because of malware and worms that used to send out email. It can be as simple as this. Have you tried telnetting from your local machine to the email server (telnet mailserver.io 25)? If this ends up in a time out you have your answer and the port is either filtered out at your ISP or from their end.
You can try this code
smtp.Host = "relay-hosting.secureserver.net"; smtp.EnableSsl = false; smtp.UseDefaultCredentials = false; smtp.Port = 25;
string From = "[MyGodaddyEMailAddress]"; //eg.info#mango.com
string FromPassword = "[MyGodaddyMailPassword]";
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(From);
msg.To.Add("[RecipientEmailAddress]");
msg.Subject = "[MailSubject]";
msg.Body = "[MailBody]";
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("mail.[domain].com", 587); //eg. mail.mango.com
smtp.Credentials = new System.Net.NetworkCredential(From, FromPassword);
smtp.EnableSsl = false;
// Sending the email
smtp.Send(msg);
// destroy the message after sent
msg.Dispose();
Console.WriteLine("Message Sent Successfully");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.ReadKey();

avast VS my Programm

I have problem with my program. I make a simple mail-messenger, and in this code :
var mail = new MailMessage();
var smtpServer = new SmtpClient("smtp.mail.ru");
smtpServer.Port = 25;
smtpServer.Credentials = new
NetworkCredential("#mail.ru", "pass");
smtpServer.EnableSsl = true;
mail.Body = text;
mail.From = new MailAddress("#mail.ru");
mail.To.Add("#mail.ru");
smtpServer.Send(mail); // in this moment
Avast find the idp generic in moment of mail send. The other antiviruses (eset32, Kaspersky,cureit) do not see problems and danger in code. I also was try to send mail with mailkit.dll. how can I fix it?
Your best bet is probably using the standard SMTP client-to-server email submission port: 587.
smtpServer.Port = 587;
If you, for whatever reason, can't use the standard email submission port, use the Avast GUI to make exceptions. Doing it through the antivirus directly should allow your program to run on your machine, and that's all you want, right? ;)
You can try:
Excluding your build directory from live scans
Authorizing your process to use port 25
Avast help pages for further reading

smtp.live.com - mailbox unavailable. The server response was: 5.7.3 requested action aborted; user not authenticated

I have read other answers on the stackoverflow. but none of the solutions work for me.
I'm trying to send email through live.com, but unable to it.
The error message:
mailbox unavailable. The server response was: 5.7.3 requested action aborted;
user not authenticated
The code:
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email#live.com");
mail.To.Add("someone#someone.com");
mail.Subject = "hello";
mail.Body = "awefkljj kawefl";
mail.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtp.live.com", 587);
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("email#live.com", "password");
smtp.Send(mail);
Are you able to send the email by using above code?
It works before, last year, but it is no more working now.
What had been changed?
I ran into an issue where I was unable to send emails using the smtp.live.com SMTP server from certain hosts -- particulary Azure hosts. In my case, the SMTP attempt was from a host that I had never used to sign-in previously, so the attempt was blocked with the 5.7.3 error:
Mailbox unavailable. The server response was: 5.7.3 requested action aborted; user not authenticated
The solution was to browse to the account settings, locate the SMTP request in its recent activity, and select "This was me":
In my case I was using a gmail account but was telling the service otherwise.
I changed:
smtp.Host = "smtp.live.com";
To:
smtp.Host = "smtp.gmail.com";
Which resolved my issue.
This Solved My Problem:
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("myaccount#live.com","mypassword");
UseDefaultCredentialsproperty should set to false before setting Credentials
If you have this problem on 2017, and you think you are probaly using an office 365 account, make sure to change the host to "smtp.office365.com". It worked for me.

Issue in receiving emails [duplicate]

This question already has answers here:
The SMTP server requires a secure connection or the client was not authenticated
(3 answers)
Closed 8 years ago.
I am doing my project in mvc4 using c#. I have a contact page i my website. My need is that i have to receive messages to my email id from other id's, when clicking the Send button.I use the following code
public void ReceiveMail(string name,string email,string message)
{
MailMessage msg = new MailMessage();
HttpContext ctx = HttpContext.Current;
msg.To.Add(new MailAddress("MyEmailId"));
msg.From = new MailAddress(email);
msg.Subject =name + "send a message";
msg.Priority = MailPriority.High;
msg.Body = message;
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");// i am confused what to write here
SmtpServer.Send(msg);
}
It shows the error
The SMTP server requires a secure connection or the client was not authenticated.
The server response was: 5.7.0 Must issue a STARTTLS command first.
at4sm42219747pbc.30 - gsmtp
I don't know from which server i got the mail. Then how can i solve this issue . Please help me
Sending emails with Gmail requires some additional settings. At first, port number should be 587 (instead of default 25). At second, Gmail requires secure connection. And of course you should provide valid credentials.
All in all, initialization of SmtpClient should look like this:
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com", 587);
SmtpServer.EnableSsl = true;
SmtpServer.Credentials = new NetworkCredential("username#gmail.com", "password");
as the error says, a STARTTLS command should be used first. Thas means gmail only accepts mail via secure connection. In this answer enableSsl was set to true. As the documentation from microsoft says, the SmtpClient class has such an property too. Furthermore you should leave your credentials in the smptClient. I think gmail only accepts mail from authenticate users. I think the whole problem is solved here.
You need to use NetworkCredential to login into Gmail SMTP server. Error is very apparent.
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("your-email", "your-password");
Have you tried:
smtpServer.Host = "smtp.gmail.com";
smtpServer.Port = 587;
smtpServer.Credentials =
new NetworkCredential("SenderGmailUserName", "SenderPassword");

Send mail using localhost SMTP

I am trying to setup SMTP server on IIS for sending mails. The SMTP server is intended to be used by the ASP.NET code in C#.
I was previously using gmail smtp wherein i provided the smtp.gmail.com as host with secure port and my gmail uid/pwd. That worked fine. Here is the code used to do that.
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
Now i am planning to use the localhost SMTP server on IIS, what values should i be giving for the parameters UseDefaultCredentials and Credentials. I will be assigning false to EnableSsl as it's over port 25.
Also, what could be the most simple SMTP virtual server configuration.
When you are using the local IIS SMTP service, set the DeliveryMethod to PickupDirectoryFromIis. For example:
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
This totally bypasses the network layer, and writes the messages directly to disk. Its much faster than going through the chatty SMTP protocol.
When you using the above code, it means you can get rid of this part of your code:
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;
I think in localhost you can use :
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Send(mailMessage);
It depends on how you configure the smtp server. You might not need to use any credentials at all, and just configure the server to only accept local connections.
Have you tried enabling relay?
Find IIS6 manager (I have found that searching for IIS may return 2 results) go to the SMTP server properties then 'Access' then press the relay button.
Then you can either select all or only allow certain ip's like 127.0.0.1
If you want to test emails in localhost, just download install the papercut tool https://papercut.codeplex.com/
and change hostname to localhost as below. Papercut captures all the emails sending using localhost.
smtpClient.UseDefaultCredentials = false;
smtpClient.Host = "localhost";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(uname,pwd);
smtpClient.EnableSsl = true;
Tx Natim, what you say worked for me. Have our intranet app using integrated auth with our exchange 2007 server now:
Dim msg As New MailMessage()
Dim smtp As SmtpClient
msg.From = New MailAddress(strFrom)
msg.To.Add(strTo)
msg.Subject = strSubject
msg.Body = strBody
smtp = New SmtpClient("ServerName")
smtp.UseDefaultCredentials = True
smtp.Send(msg)

Categories

Resources