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();
Related
I have a web page, In CONTACT US tab I have a forms user can only enter their Name, Email Id, Subject and message. Once they click the ok button I want to get those message to my hotmail account.
I tried some code. But it doesn't work.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(txtUserEmail.text, txtPassword);
smtp.EnableSsl = true;
MailMessage msg = new MailMessage();
msg.Subject = "Demo";
msg.Body = "Hi there..";
string toAddress = "xxx#hotmail.com"; // Add Recepient address
msg.To.Add(toAddress);
string fromAddress = "\"no reply \" <from#gmail.com>";
msg.From = new MailAddress(fromAddress);
msg.IsBodyHtml = true;
try
{
smtp.Send(msg);
}
catch
{
throw;
}
This code I tried. But it's having a password. I want user to send email without password to my xxx#hotmail.com
Here is my form design
That depends on your host. Usually web hosts give you a local SMTP server, then you can use it to send any mail you want, just need to know the configuration settings and use them with the SmtpClient.
If your host doesn't offer smtp (very strange unless you're selfhosting the page) you can:
1-Install a local SMTP server (if you manage the server), this is the preferred solution.
2-Use an external service like google to send the mails, but then you need to create an account on the service and use these credentials, and have in account that Google has a lot of restrictions sending emails (limit per second, marking mails as spam, etc etc).
Preferred way to do this is to not have the mail be sent from the users e-mail, but rather have a dummy e-mail that sends the mails and contains the data the user entered. Not the best solution probably, but it doesn't require user credentials.
I want my application to send e-mail using 'SMTP over SSL' even if TLS is not supported by server.
So far I have tried
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("abc#xyz.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true; //true: sends using TLS, false: sends without security
SmtpServer.Send(mail);
MessageBox.Show("Mail sent");
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.ToString());
}
by setting the property called EnableSsl, I can send mail over the servers which support TLS but I am not able to send it through server which only supports SMTP over SSL.
How can I give support for this SMTP/SSL method?
According to the SMTPClient spec:
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.enablessl(v=vs.110).aspx
The SmtpClient class only supports the SMTP Service Extension for Secure SMTP over Transport Layer Security as defined in RFC 3207. In this mode, the SMTP session begins on an unencrypted channel, then a STARTTLS command is issued by the client to the server to switch to secure communication using SSL. See RFC 3207 published by the Internet Engineering Task Force (IETF) for more information.
You can try using System.Web.Mail.SmtpMail, which is deprecated, but which supports SSL:
https://msdn.microsoft.com/en-us/library/system.web.mail.smtpmail(v=vs.110).aspx
TBH I think you should place a caveat on your service and state that only SMTP servers that use TLS are supported. But at the end of the day, that is up to you.
This link shows one more way that I can send email using SMTP over SSL with the help of Collaboration Data Objects component. This way also supports embedding images to email.
Please change your code..
SmtpServer.EnableSsl = false;
I am sending mail using this code
using System.Net.Mail;
using System.Net.Security;
MailMessage mail = new MailMessage();
mail.From = new MailAddress("abc#gmail.com");
mail.To.Add("xyz#gmail.com");
mail.IsBodyHtml = true;
mail.Subject = "Email Sent";
mail.Body = "Mail Done";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "123456");
smtp.EnableSsl = true;
smtp.Send(mail);
Label1.Text = "Mail Sent";
Whem I am using abc#gmail.com(one email id) for sending email, mail will successfully send but when I am using pqr#gmail.com(another mail id) mail sending failed. On local server both "abc" & "pqr" working fine.
Please help me to sort out this problem.
Error Message
the smtp server requires a secure connection or the client is not authenticated the server response was 5.5.1 authentication requires
Update you code to the following:
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("abc#gmail.com", "123456");
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Try this line after the enableSSl=true code
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
Gmail has added a new security feature for not to allow sending email using the less secure apps. You have to change the settings for your gmail account to allow access by less secure apps using steps given by google at below link
https://support.google.com/a/answer/6260879?hl=en
Enable less secure apps to access accounts
1.Sign in to your Google Admin console.
Click Security > Basic settings.
Under Less secure apps, select Go to settings for less secure apps.
In the subwindow, select the Allow users to manage their access to less secure apps radio button.
Once you've set Allow users to manage their access to less secure apps to on, affected users within the selected group or Organizational Unit will be able to toggle access for less secure apps on or off themselves.
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");
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)