This question already has answers here:
Sending an email to the local domain
(3 answers)
Closed 4 years ago.
I am trying to figure out why I can't send a simple mail with attachment using a office365 account, I don't if my code has something wrong or configuration from account is missing, please help with the code:
code is:
protected void sentmail(string sb)
{
using (var msg = new MailMessage())
{
msg.To.Add(new MailAddress("myaccount#company.cl));
msg.From = new MailAddress("botmail#company.cl");
msg.ReplyToList.Add("botmail#company.cl");
msg.Subject = "Test Mail";
msg.Body = sb.ToString();
msg.IsBodyHtml = true;
var client = new SmtpClient
{
Host = "smtp.office365.com", //it worked on testmail
Credentials = new System.Net.NetworkCredential("botmail#company.cl, "password"),
Port = 25,
EnableSsl = true
};
client.Send(msg);
}
}
Error on sending is:
5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [SC1PR80CA0082.lamprd80.prod.outlook.com]
This is production code that I have in place that will do the sending for you
using (var msg = new MailMessage())
{
msg.To.Add(new MailAddress(userName));
msg.From = new MailAddress(userName);
msg.ReplyToList.Add(model.EmailAddress);
msg.Subject = "Message from the Web";
msg.Body = sb.ToString();
msg.IsBodyHtml = true;
var client = new SmtpClient
{
Host = "xxxmydomainxxx-co-uk.mail.protection.outlook.com",
Credentials = new System.Net.NetworkCredential(userName, password),
Port = 25,
EnableSsl = true
};
// You can use Port 25 if 587 is blocked
try
{
client.Send(msg);
return true;
}
catch (SmtpException smtpEx)
{
return smtpEx;
}
catch (Exception ex)
{
return ex;
}
}
It works without attachment?
Try add the host in credentials:
credentials = new System.Net.NetworkCredential(MAIL_CREDENTIALS_EMAIL, PASS_CREDENTIALS_EMAIL, HOST_EMAIL);
Related
I am trying to use the code snippet below. The goal is to just send an email, however I am getting an error "The SMTP server requires a secure connection or the client was not authenticated.".
My question is. What is the best way to send emails via code if an SMPT server requires an authenticated account? Is it bad practice to have a developer type account created for this purpose?
string server = "";
string to = "";
string from = "";
MailMessage message = new MailMessage(from, to);
message. Subject = "Subject";
message. Body = "Body";
SmtpClient client = new SmtpClient(server);
client.UseDefaultCredentials = true;
client. Port = 000;
client.EnableSsl = true;
This is how I send email using System.Net.Mail through office365 smtp.
Yes, you need to have an account and password with office365 to be able to send email through their smtp server. Use the code below, it is the same one I use in my web app to send email to users.
using (var message = new MailMessage())
{
message.To.Add(new MailAddress("recepient email", "receipient name"));
message.From = new MailAddress("your email", "your name");
message.Subject = "My subject";
message.Body = "My message";
message.IsBodyHtml = false; // change to true if body msg is in html
using (var client = new SmtpClient("smtp.office365.com"))
{
client.UseDefaultCredentials = false;
client.Port = 587;
client.Credentials = new NetworkCredential("your email", "your password");
client.EnableSsl = true;
try
{
await client.SendMailAsync(message); // Email sent
}
catch (Exception e)
{
// Email not sent, log exception
}
}
}
I'm trying to implement a mail service to use it with sending reset password email, I tried to use Gmail and it doesn't work so I switched to outlook but it still not working. Can anybody help? Thanks
private SmtpClient _client;
public StringBuilder _body;
public EmailService()
{
_body = new StringBuilder();
_client = new SmtpClient();
}
public void Dispose()
{
_body.Clear();
_client.Dispose();
}
public async Task<bool> SendEmailAsync(string fullname, string receiverEmail, string subject)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(receiverEmail);
mail.From = new MailAddress("testmoenergy#outlook.com", "Aljawhara", Encoding.UTF8);
mail.Subject = subject;
mail.Body = _body.ToString();
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
_client.Host = "smtp.outlook.com";
_client.Port = 587;
_client.UseDefaultCredentials = false;
_client.Credentials = new NetworkCredential("testmoenergy#outlook.com", "test******");
_client.EnableSsl = true;
await _client.SendMailAsync(mail);
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
The Gmail oauth is not allowing users to send mail from 1-may 2022. you have to create a auth key from you gmail account and use it as password.
string username = "yourmailID";
string password = "yourauthcode";
ICredentialsByHost credentials = new NetworkCredential(username, password);
SmtpClient smtpClient = new SmtpClient()
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
Credentials = credentials
};
MailMessage mail = new MailMessage();
mail.From = new MailAddress(username);
mail.To.Add(username);
mail.Subject = subject;
mail.Body = body;
smtpClient.Send(mail);
for generate auth code
In gmail go to you account settings--> security-->enable two step verfication to ON -->app passwords--> give you custom app name and click generate .
This will give your auth code.
I am trying to test sending out emails to users from a generic email such as noreply#company.com.
I want to do this in a button click event. How do I achieve this without using credentials?
I have tried several things but I always get errors.
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
mm.To.Add(new System.Net.Mail.MailAddress("Email Address", "Name"));
mm.From = new System.Net.Mail.MailAddress("Email Address");
mm.Sender = new System.Net.Mail.MailAddress("Email Address", "Name");
mm.Subject = "This is Test Email";
mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
mm.IsBodyHtml = true;
mm.Priority = System.Net.Mail.MailPriority.High; // Set Priority to sending mail
System.Net.Mail.SmtpClient smtCliend = new System.Net.Mail.SmtpClient();
smtCliend.Host = "Your smtp server";
smtCliend.Port = 25; // SMTP port no
smtCliend.Credentials = new NetworkCredential("User Name", "Password");
smtCliend.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
smtCliend.Send(mm);
}
catch (System.Net.Mail.SmtpException ex)
{
lblMsg.Text = ex.ToString();
}
catch (Exception exe)
{
lblMsg.Text = "\n\n\n" + exe.ToString();
}
Thanks
When I send mail to my gmail account, it shows below error.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Authentication required...
code I am using is below
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();
try
{
m.From = new MailAddress("me#gmail.com");
m.To.Add("me#gmail.com");
m.Subject = "This is a Test Mail";
m.IsBodyHtml = true;
m.Body = "test gmail";
sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new System.Net.NetworkCredential("me#gmail.com", "passward");
sc.UseDefaultCredentials = true;
sc.EnableSsl = true;
sc.Send(m);
Response.Write("Email Send successfully");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Just tried your code, had to fiddle with a couple things but was sent this. Funny because I have done this previously using Gmail smtp (couple years back). But it looks like they are now verifying apps that use their platform.
Either use another smtp server that you are signed up to, or use your own. (there must be a test one that is available online??). Pretty sure sendgrid do a free trial.
using System.Net;
using System.Net.Mail;
string smtpAddress = "smtp.mail.yahoo.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "email#yahoo.com";
string password = "abcdefg";
string emailTo = "someone#domain.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// Can set to false, if you are sending pure text.
mail.Attachments.Add(new Attachment("C:\\SomeFile.txt"));
mail.Attachments.Add(new Attachment("C:\\SomeZip.zip"));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Please try this this should work for you
Thank you
I have write C# code to send mail (my company mail). I tried with gmail and it working but with my company mail is not.
I sure the smtp server is running and port 465 opened since I can send mail by outlook 2k7 with the same account, telnet smtp.domain 465 ok.
When i run the code it throw exception "System.Net.Mail.SmtpException: The operation has time out."
Here is my c# code:
var fromAddress = new MailAddress("ID#domain", "Display Name");
var toAddress = new MailAddress("ID#domain", "Display Name");
const string subject = "Test mail";
const string body = "Test mail";
var smtp = new SmtpClient
{
Host = "smtp.domain",
Port = 465,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential("ID", "pass"),
Timeout=15000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
})
{
try
{
smtp.Send(message);
MessageBox.Show("OK");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Any one know or have same problem that fixed please help me. Thanks so much!
Try changing setting EnableSsl = false in your SmtpClient instance.
You can change this code and reuse this code:
public static void sendEmail(string address, string subject, string message)
{
string email = "yourEmail";
string password = "yourPass";
var loginInfo = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", portNumber);
msg.From = new MailAddress(email);
msg.To.Add(new MailAddress(address));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = loginInfo;
smtpClient.Send(msg);
}