Error while using Office365 SMTP in Asp.Net - asp.net

I am using Office365 web app SMTP to send email from Asp.Net but Its always throwing following error!
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated at System.Net.Mail.MailCommand.CheckResponse
The web.config is given below - with username & password changed
<network enableSsl="true" host="pod51007.outlook.com" userName="XXXX" password="XXXXX" port="587" defaultCredentials="false" />

I had the same problem and solved so:
Dim client As SmtpClient = New SmtpClient()
client.Credentials = New System.Net.NetworkCredential("your user", "your password")
client.Port = 587
client.Host = "smtp.office365.com"
important are this instruction, without them is not working :
client.UseDefaultCredentials = False
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.EnableSsl = True

I think the error message is misleading (as with most Microsoft errors!) - The error code looks very simular to a SMTP error and 5.7.1 is a relay error. You may need to specify your development/production public IP address using the admin portal provided by Microsoft.

Related

Emailing from ASP.NET using Office 365 Online Exchange

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.

How do i set up asp.net web forms app to send smtp email on windows 7 so i can test?

I am trying to test send email thru smtp in asp.net web forms app on windows 7. I get timeout. I am using my fastmail account to test with. I am pretty confused at this point as i am not sure if i also need to set up smtp email thru my iis console manager. Here is the settings i have in my console manager first. I am confused to how this process works. Does asp.net app first send to smtp email then send it to fastmail smtp?
smtp email page
e-mail address: "is set to my fastmail address"
deliver email to smtp server is checked.
smtp server: mail.messagingengine.com
use localhost is unchecked
port: 25 fastmail port is 465 but was told to set this to 25
authentication settings: i have this set to my email address and password
here is the session state page
session state mode settings: in process
cookie settings
mode: use cookies
name: asp.net_sessionid
use hosting identity for impoersonation is checked
next here is my web.config file in the asp.net app
<system.net>
<mailsettings>
<smtp deliveryMethod="Network" from"roger <myname#fastmail.us>">
<network host="mail.messagingengine.com" userName="myname#fastmail.us" password="myemailpassword" port="465" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
next here is my c# behind code
protected void btn_send_Click(object sender, EventArgs e)
{
try {
string sendername = txtbx_name.Text.ToString();
MailMessage mymessage = new MailMessage();
mymessage.Subject = txtbx_subject.Text.ToString();
mymessage.Body = txtbx_message.Text.ToString();
mymessage.From = new MailAddress("george#georgebush.me", sendername);
mymessage.To.Add(new MailAddress("myemail address", "Roger"));
SmtpClient mysmtpclient = new SmtpClient();
mysmtpclient.UseDefaultCredentials = false;
mysmtpclient.DeliveryMethod = SmtpDeliveryMethod.Network;
mysmtpclient.Send(mymessage);
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true);
}
catch (Exception ex)
{
string message = ex.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + message + "');", true);
}
}
am assuming the html and css isnt needed here. Any help is appreciated. I cant even get an error to catch in try catch statement. I am pretty lost at this point as i am new to asp.net.
I guess your code is correct, you must add address for:
mymessage.To.Add(new MailAddress("myemail address", "Roger"));
Instead of "mymail address" just put an email address
Did you check your setting file, you forgot an equal mark after from as below:
<smtp deliveryMethod="Network" from"roger <myname#fastmail.us>">

ASP.NET Send Email

I am trying to send a email when user clicks submit in a contact us page but for some reason its not working, what am I doing wrong? (PS email & password was omited from this code snippet but included in actual solution.
Thanks
Code in web.config:
<system.net>
<mailSettings>
<smtp from="order#test.com">
<network host="smtp.gmail.com"
userName="" //my email
password="" //password deleted for privacy reasons
defaultCredentials="false"
port="456"
enableSsl="true" />
</smtp>
</mailSettings>
code in asp.net contact form:
protected void btnSubmit_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add(new MailAddress("test#gmail.com"));
mail.Subject = "Test";
mail.Body = "Message was sent from" + txtName.text + txtComment.text;
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
}
Gmails uses port 465, not 456 for SSL SMTP. From here:
Outgoing Mail (SMTP) Server - requires TLS1 or SSL: smtp.gmail.com
Use Authentication: Yes
Port for TLS/STARTTLS: 587
Port for SSL: 465
The "other" possible reason:
Does your code "work" when developing locally, but stops working when you "publish"/ftp/copy, etc. your files to your web host?
If Yes: check your host's trust settings for ASP.Net. If it's medium trust (which is likely in shared hosting), then note that you cannot use any port other than port 25 for SMTP in medium trust.
It works locally (dev) because in local dev/VS environment, ASP.Net runs in full trust.
REF (MSDN Blogs): SMTP Problems when ASP.Net is not running in full-trust

Sending email using ASP.NET I am getting this 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. u6sm344516ibd.6
I have my code like this?
MailAddress to = new MailAddress("xxxxx#gmail.com");
MailAddress from = new MailAddress("xxx#gmail.com");
MailMessage message = new MailMessage(from, to);
message.Subject = "Error Occred in the application:";
message.Body = ex.Message;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
the SMTP server required that you use a secure connection
client.EnableSsl = true;
You could check if setting the EnableSsl property for SmtpClient to true and specifying the credentials would help.
client.EnableSsl = true;
client.Credentials = new NetworkCredential("user", "password");
You will need to set the appropriate properties of the SmtpClient instance to enable TSL/SSL, and set credentials. Check out this for more:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
Try configuring your SMTP to use port 25 (with SSL).
For gmail Smtp server, use port 587. Port 465 has problems. Make sure you also pass in your correct gmail address and the password that you use with that address/account. Finally, ensure you have set your gmail account to accept connections from other email apps.

Email Code Error

In Email code, I get following error.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
Is this indicating that we require SSL certificate ?
This error code means that you need to provide a valid credentials in order to use this smtp server:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount#gmail.com", "secret");

Resources