EDIT: I'll add that the domain is hosted by Web.com. Perhaps someone else uses them and has experienced something similar.
The code below is set up for both my own smtp server and gmail. Both fail for different reasons. I'm writing this in VS 2010/.Net 4
When I send from my own domain smtp server I receive no exceptions I just never send any email. I have tried sending from different accounts to different accounts. There are no exceptions, is just never gets delivered. I changed my password and did receive and authentication error, which seems to prove I had the credentials correct. I am following my domain hosts instructions for domain name and port.
With gmail I get the error, "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at". I read about this and followed the links to allow less secure devices to access my gmail. I received the confirmation email from gmail indicating I had made the changes for less secure email, but I still get the same error.
I have read and tried everything at the following links and can't get past this.
http://ithoughthecamewithyou.com/post/sending-email-via-gmail-in-cnet-using-smtpclient
c# SmtpClient class not able to send email using gmail
http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
When looking at the code I am using, does anything jump out at you and being not right? Is there a property set wrong or am I missing a Property?
Public Shared Sub SendMailMessage(ByVal sfrom As String, ByVal recepient As String, ByVal bcc As String, ByVal cc As String, ByVal subject As String, ByVal body As String)
Try
Dim mMailMessage As New MailMessage()
mMailMessage.From = New MailAddress(sfrom)
mMailMessage.To.Add(New MailAddress(recepient))
If Not bcc Is Nothing And bcc <> String.Empty Then
mMailMessage.Bcc.Add(New MailAddress(bcc))
End If
If Not cc Is Nothing And cc <> String.Empty Then
mMailMessage.CC.Add(New MailAddress(cc))
End If
mMailMessage.Subject = subject
mMailMessage.Body = body
mMailMessage.IsBodyHtml = True
mMailMessage.Priority = MailPriority.Normal
'GMAIL
'Dim mSmtpClient As New SmtpClient()
'mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
'mSmtpClient.UseDefaultCredentials = True
'mSmtpClient.EnableSsl = True
'mSmtpClient.Host = "smtp.gmail.com"
'mSmtpClient.Port = 587
'mSmtpClient.Credentials = New System.Net.NetworkCredential("my gmail address", "my password")
'mSmtpClient.Send(mMailMessage)
'MY DOMAIN
Dim mSmtpClient As New SmtpClient()
mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
mSmtpClient.UseDefaultCredentials = True
mSmtpClient.EnableSsl = False
mSmtpClient.Host = "smtp.mydomain.com"
mSmtpClient.Port = 587
mSmtpClient.Credentials = New System.Net.NetworkCredential("my full email address", "my password")
mSmtpClient.Send(mMailMessage)
Catch ex As Exception
'HttpContext.Current.Response.Redirect("~\EmailError.aspx")
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
End Sub
For gmail my password was too weak. Ugh! Why couldn't Google just tell me that instead of saying, "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
It was an authentication error, so the message was accurate, just misleading. 6 hours of my life I'll never get back. Oh, plus the useless half hour I spent on the phone with Web.com support.
Related
Trying to send email from asp.net application.
Emailing to server address: Smtp.office365.com
Port - 587
Connection Security - STARTTLS
I have SMTP user name and SMTP password.
Fails to send email.
The code uses the SmtpClient, as follows:
String userName = "name#example.com";
String password = "Password";
MailAddress RecipientEmail = new MailAddress(EmailTo);
MailAddress SenderEmail = new MailAddress(cEmailFrom);
MailMessage msg = new MailMessage(SenderEmail, RecipientEmail);
msg.Subject = "Test";
msg.Body = "This is a test";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.office365.com:587");
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.EnableSsl = true;
client.Send(msg);
The error message: Failure sending email. No other information.
But if I specify the port:
client.port = 587
instead of adding it to the host name, I get a very long error:
Transaction failed. The server response was: 5.2.0
STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:BC110000, 17.43559:0000000094000000000000000100000000000000, 20.52176:140FBF85130010100A00D231, 20.50032:140FBF85831710100A00E231, 0.35180:86260000, 255.23226:0A00C931, 255.27962:0A000000, 255.27962:0E000000, 255.31418:0A000000, 16.55847:DC000000, 17.43559:0000000088010000000000001E00000000000000, 20.52176:140FBF85130010109F260000, 20.50032:140FBF8583171010A4260000, 0.35180:0A00D330, 255.23226:A9260000, 255.27962:0A000000, 255.27962:32000000, 255.17082:DC040000, 0.27745:B3260000, 4.21921:DC040000, 255.27962:FA000000, 255.1494:0A007530, 0.37692:02010480, 0.44092:00000000, 0.40348:02010480, 0.34608:04000100, 0.55056:00000000, 0.42768:302E3134, 0.56112:31393A44, 0.52807:30363031, 4.33016:DC040000, 7.40748:010000000000010B2D343438, 7.57132:000000000000000037323032,
What is missing?
The important part from the error is the phrase "SendAsDenied".
Office 365 won't let you use your account on their smtp servers to send e-mail messages from someone else's address. You just can't do it. The closest you can get is in cases of organizational domain accounts, you can sometimes have service accounts within the organization that can be delegated to send on behalf of other users within the organization's domain.
If you need to do more than that, you must manage your own smtp server... and let me tell you, that's a whole other can of worms, requiring an ability to understand and configure some or all of rDNS, SPF, DKIM, Domain-Keys, and DMARC.
I set up SendGrid account and got key and pw.
My VisualBasic 2015 console app runs DeliverAsync without error, but email doesn't get to Internet receipients (my Hotmail account).
Also, the task.wait() throws exception "Bad username / password", which is posted at the end
Wireshark on Azure shows no SMTP, but I don't know if SendGrid uses SMTP.
Here is the app:
' Create the email object first, then add the properties.
Dim myMessage As SendGridMessage
myMessage = New SendGridMessage()
' Add the message properties.
myMessage.From = New MailAddress("<my email addr>")
' Add multiple addresses to the To field.
myMessage.AddTo("<destination email addr 1>")
myMessage.AddTo("<destination email addr 2>")
myMessage.AddTo("<destination email addr 3>")
myMessage.Subject = "Testing the SendGrid Library 2"
'Add the HTML and Text bodies
myMessage.Html = "<p>Hello World!</p>"
myMessage.Text = "Hello World plain text!"
Dim credentials As NetworkCredential
credentials = New NetworkCredential("apikey", "<my api pw>")
transportWeb = New Web(credentials)
Dim task = transportWeb.DeliverAsync(myMessage)
Try
task.wait()
Catch ex As AggregateException
Stop '<<<<<<<<< I GET: "Bad username / password"
Catch
End Try
EXCEPTION DETAILS:
"Bad username / password"
DeliverAsync returns a Task, so you need to await the task.
Await transportWeb.DeliverAsync(myMessage)
Of course, to use the await keyword your method needs to be marked as async. If you don't want to do that, then you can manually wait on the task.
Dim task = transportWeb.DeliverAsync(myMessage)
task.Wait()
You should familiarize yourself with the Task-based Asynchronous Pattern (TAP). Often when a function name ends in -Async then it uses TAP.
I got it working by creating new VB web app instead of win app.
VB > create new proj > web app > MVC and then props > references > NU.. Mgr > search SendGrid > Install, and that's it.
I am producing a website for a friends company and they would like customers to be able to contact them using a form on the website that will email the query to their work email address.
All the examples I have been able to find use GMail's SMTP server. I have managed to get this to work using the code below but e-mails always appear in the inbox as from the GMail account, regardless of what I set mm.From to be. I understand that this is the downside to using GMail's SMTP server.
Dim mm As MailMessage = New MailMessage()
mm.From = New MailAddress("customer#test.com")
mm.Subject = "Test Email"
mm.Body = "<p>This is a test email</p>"
mm.IsBodyHtml = True
mm.To.Add(New MailAddress("info#myCompany.com"))
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As NetworkCredential = New System.Net.NetworkCredential()
NetworkCred.UserName = "mycompany#gmail.com"
NetworkCred.Password = "myPassword"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
Try
smtp.Send(mm)
Catch ex As Exception
Response.Write(ex)
End Try
I would like the senders e-mail address to appear in the from field so users can just click reply when a message comes in.
The business e-mail account they have is provided by GoDaddy and I have tried entering the SMTP and account details in the code but I get the following error:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
Any help would be greatly appreciated.
Thanks
You can use any SMTP service that you have access to. So if GMail's doesn't do what you need, try another one. (The settings for that other one may be different, check with the service host for details.)
However, one thing you can definitely do in your code is set a ReplyTo address. From is only one header in the message, there are others that can be used. A ReplyTo header tells the email client that replies should be sent to a given email address.
mm.ReplyTo = New MailAddress("customer#test.com")
WebMail.Send(to: "someone#somewhere.com", subject: "email subject", body: "message body, isBodyHtml: false );
WebMail.SmtpServer = "smtp.gmail.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "me#mydomain.com";
WebMail.Password = "the password";
WebMail.From = "me#mydomain.com";
By using the above method, I am receiving email from sender name as "me#mydomain.com".. I tried changing the parameters "userName" and "From", but the gmail server rejects the message.
What parameter to add to receive email from a sender name instead of email id..
try this:
System.Web.Mail is not a full .NET native implementation of the SMTP
protocol. Instead, it uses the pre-existing COM functionality in
CDONTS. System.Net.Mail, in contrast, is a fully managed
implementation of an SMTP client.
So In MailAddress From = new MailAddress(FromName, FromName);
^
From = Gets or sets the e-mail address of the sender
Source
Not sure if this question has been asked elsewhere...
For a website project that I'm working on, I created an ASPX web form that will send an email containing the contents of the form on button click. I tried it and it worked when I'm using the Yahoo SMTP server and testing using my personal Yahoo email address (+ specifying the username and password).
However, it wouldn't work when I'm using the SMTP server for mail messages for the website (after I've created a new admin email account in the website).
The website is up and running and I'm able to send an email to the website's admin email account (through Yahoo) but just not when sending through the ASPX web form.
Here is the code for using the Yahoo SMTP server and it works fine:
SmtpServer.Credentials = New Net.NetworkCredential("username#yahoo.com", "password")
SmtpServer.Port = "25"
SmtpServer.Host = "smtp.mail.yahoo.com"
mail.From = New MailAddress("username#yahoo.com")
mail.To.Add("username#yahoo.com")
mail.Subject = "RE: Message Subject"
mail.Body = "Message goes here."
SmtpServer.Send(mail)
Now, here is the code for using my website's SMTP server which doesn't work:
SmtpServer.Port = "25"
SmtpServer.Host = "127.0.0.1"
mail.From = New MailAddress("admin#awesomewebsite.org.au")
mail.To.Add("admin#awesomewebsite.org.au")
mail.Subject = "RE: Message Subject"
mail.Body = "Message goes here."
SmtpServer.Send(mail)
These are the correct port and host numbers given by the web hosting company that hosts the website. They also said that username and password are not needed. I also tried appending this to the beginning of the code but it still wouldn't work:
SmtpServer.Credentials = New Net.NetworkCredential()
or even
SmtpServer.Credentials = New Net.NetworkCredential("", "")
I really don't know what to do. Please help this newbie out. Any response is appreciated.
By the way, the error message that I get when I test this out appears in a message box: "Failure sending mail."
You need to create an email account in your web site and use those credentials to send the mail just as you did with yahoo (This can be done in your admin portal of the website but some hosting providers will charge you separately for the email service). Further more the correct smtp server is also should be given, that depends on your hosting provider. You can get that information from your hosting provider. 127.0.0.1 will not work here anyway.
Don't import 'System.Net.Mail'.
Completed code is shown below:
Dim SmtpServer As New Net.Mail.SmtpClient
Dim mail As New Net.Mail.MailMessage
SmtpServer.Host = "198.154.99.8"
SmtpServer.UseDefaultCredentials = False
SmtpServer.Credentials = New System.Net.NetworkCredential("admin#awesomewebsite.org.au", "password")
mail.From = New Net.Mail.MailAddress("admin#awesomewebsite.org.au")
mail.To.Add("admin#awesomewebsite.org.au")
mail.Subject = "RE: Message Subject"
mail.Body = "Message goes here."
SmtpServer.Send(mail)