asp.net c# Web Forms send email using outlook 365 - asp.net

this was my code for my local machine before my company switched to outlook 365:
SmtpClient SMTP Client = new SmtpClient(ConfigurationManager.AppSettings["EMailServer"]);
smtpClient.Send(message);
Since the outlook was changed, I google and find this code, however it does not work giving error code "Failed to send email" "General failure".
Please help me to resolve this issue
here is a new code:
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredentmailial("email address", "network password");
client.Port = 25; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
//client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.TargetName = "STARTTLS/smtp.office365.com";
client.Send(message);
Thank you very much.
Vitaly.

This is the code I use to send the email using HOST smtp.office365.com
var fromAddress = new MailAddress("from#domain.com", "From Name");
var toAddress = new MailAddress("to#domain.com", "To Name");
string fromPassword = "Your_email_password";
string subject = "Test Subject";
string body = "Test Message";
var smtp = new SmtpClient
{
Host = "smtp.office365.com",
Port = 25, //Recommended port is 587
EnableSsl = false,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
{
smtp.Send(message);
}
Please check that is your port 25 is blocked by OS or not.

Related

asp.net sending mail through office 365 smtp

I'm trying to set up a email notification for a asp.net project, but it doesn't seem to work. On localhost it gives me the following error
5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
while on azure it triggers a suspicious login message over and over.
I tried multiple suggestions:
port 25 instead of 587
UseDefaultCredentials true or false
enable and disable SSL
nothing seems to work. The suspecious activity mail is the closest I got so far. Here is my code
var client = new SmtpClient();
client.Host = "smtp.office365.com";
client.UseDefaultCredentials = false;
client.Port = 587;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("Rachelle.XXXX#outlook.com", "password");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
var message = new MailMessage("Rachelle.XXXX#outlook.com", "Rachelle_xxxxx#hotmail.com");
message.Subject = "a ticket has been changed";
message.Body = "Hi there, a ticket has been changed on Cronos. Please check Cronos for more details";
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
try
{
client.Send(message);
return message;
}

I can't send email in asp .net

When i send email there is an exception which says "failure sending mail".
var mailObj = new MailMessage("marabifuad2013#gmail.com", "to Email");
mailObj.Subject = "subject";
mailObj.Body ="";
mailObj.IsBodyHtml = true;
var smtpServer = new SmtpClient("smtp.gmail.com", 587);
smtpServer.Credentials = new NetworkCredential("marabifuad2013#gmail.com", "password");
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.Send(mailObj);
I guess that because you haven't specified to use SSL:
smtpServer.EnableSsl = true;
Also you can try:
smtpServer.UseDefaultCredentials = true;
Apart from that everything looks fine, providing that you entering correct credentials and etc.

Unable to send Emails from Remote Server using asp.net & C#?

I am able to send emails successfully from LocalHost.
After hosting my website i am unable to send.
The following is the error:
System.Net.Mail.SmtpException: 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. ha10sm40374374pbc.23 - gsmtp
can anyone help me to solve this.
Below is code:
public void SendMail(string ToMail, string subject, string Message)
{
// Gmail Address from where you send the mail
string fromAddress = "mygmailid#gmail.com";
// any address where the email will be sending
//string toAddress = ToMail;
//Password of your gmail address
const string fromPassword = "********";
// Passing the values and make a email formate to display
//string subject = subject;
string body = "\n\n"+Message;
// smtp settings
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
// Passing values to smtp object
smtp.Send(fromAddress, ToMail, subject, body);
}
Google sent a mail with the subjact "Suspicious sign in prevented"
I selected it's me only option.
Then changed the Gmail password and used it in the application .
Now able to send the mails from the production server also...
Try below code:
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(email);
mailMessage.Subject = "YOUR_SUBJECT";
mailMessage.Body = "YOUR_MAIL_BODY";
mailMessage.To.Add(new MailAddress("RECIPIENT_EMAILID"));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new
System.Net.NetworkCredential();
NetworkCred.UserName = "SENDER_MAIL";
NetworkCred.Password = "SENDER_MAIL_PASSWORD";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Send(mailMessage);
}
After invoking above method GOOGLE or GMAIL will send you one mail regarding security (To allow the less secure app to configure mail) then allow that one.

Send email with System.Web.Mail

I want send email in asp.
I use this code
using System.Web.Mail;
MailMessage msg = new MailMessage();
msg.To = "aspnet#yahoo.com";
msg.From = "info#mysite.com";
msg.Subject = "Send mail sample";
msg.BodyFormat = MailFormat.Html;
string msgBody="Hello My Friend. This is a test.";
msg.Body = msgBody ;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(msg);
But i get error :
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
How to send email with asp?
I use This code .
MailMessage msg = new MailMessage();
msg.Body = "Body";
string smtpServer = "mail.DomainName";
string userName = "info#mysite.com";
string password = "MyPassword";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
if (userName.Length > 0)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
msg.To = user.Email;
msg.From = "info#Mysite.com";
msg.Subject = "Subject";
msg.BodyEncoding = System.Text.Encoding.UTF8;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg);
You might need to provide credentials.
example:
smtpMail.Credentials = new NetworkCredential("username", "password")
If you are trying to send email without authenticating, I am afraid that's impossible. If any users in your web site can send emails without password, that's horrible. It will allow user to send email from other person account. So considering security, sending email will required to provide email address and password
var fromAddress = ""; // Email Address here. This will be the sender.
string fromPassword = ""; // Password for above mentioned email address.
var toAddress = "";// Receiver email address here
string subject = "Hi";
string body = "Body Text here";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com"; // this is for gmail.
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(fromAddress, toAddress, subject, body);
[Edited]
Sorry My mistake i didn't noticed that. They both used for same purpose. If you are using higher version (2.0 or later) of .Net framework, then use System.Net.Mail. If you use System.Web.Mail it only shows a warning saying this is deprecated. But that will work.
Here is the answer for System.web.mail
MailMessage mail = new MailMessage();
mail.To.Add("to#domain.com");
mail.From = new MailAddress("from#domain.com");
mail.Subject = "Email using Gmail";
mail.Body = "";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(mail.From,"YourPassword");
smtp.Send(mail);

cannot send email from asp.net

It works fine for a particular email address, but rest of the emails dont works..following error shows up:
Server Error in '/RealTimeArsenicDataVisualizer' Application.
Mailbox name not allowed. The server response was: From address not verified - see http://help.yahoo.com/l/us/yahoo/mail/original/manage/sendfrom-07.html
my code:
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.mail.yahoo.co.in", 587);
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(emailid, password);
client.Port = 587;
client.Host = "smtp.mail.yahoo.co.in";
client.EnableSsl = false;
object userstate = msg;
client.Send(msg);
Change your code to below:
client.Port = 465;
client.Host = " smtp.mail.yahoo.com";
client.EnableSsl = true;

Resources