Exchange for mail usage - c#

Send an email with your domain credential of outlook:
With the following details:
POP Settings
Server: outlook.office365.com
Port: 995
Encryption: SSL/TLS
SMTP Settings
Server: smtp.office365.com
Port: 587
Encryption: STARTTLS
How to create an email service with the exchange in .net core.
MailMessage mailMessage = new MailMessage(_options.Value.Mailbox, receiverEmail, subject, emailBody);
mailMessage.IsBodyHtml = true;
mailMessage.BodyEncoding = UTF8Encoding.UTF8;
mailMessage.CC.Add(_options.Value.Mailbox);
// SmtpClient client = new SmtpClient("smtp.office365.com", 587);
SmtpClient client = new SmtpClient(_options.Value.Server, _options.Value.Port);
client.EnableSsl = true;
client.Timeout = 100000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
//client.Credentials = new NetworkCredential(senderEmail, senderPassword);
client.Credentials = new NetworkCredential(_options.Value.Email, _options.Value.Password,
_options.Value.Server);
//MailMessage mailMessage = new MailMessage(senderEmail, receiverEmail, subject, emailBody);
await client.SendMailAsync(mailMessage);
return true;
I need to send emails using an exchange server.

Related

Why would an email sent from C# be received by the destination email but not by the CC?

I am using C#. I have a method in which the SMTP client is configured and an email is sent:
MailMessage mail = new MailMessage("myEmail#myDomain.com", "otherEmail#google.com");
mail.CC.Add("otherEmailInMyDomain#myDomain.com");
mail.Subject = "Normal Subject";
mailMessage.Body = "Normal Body";
SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.Host = "smtp.office365.com";
client.Send(mailMessage);
The email is being received by the otherEmail#google.com (to which it is being sent directly), but is not being received by the otherEmailInMyDomain#myDomain.com.
This error only happens in the server, whenever i try this code in my machine, it works perfectly.

How to send email using two factor authentication enabled office365 email account in asp.net C#?

I am trying to send email using office365 e-mail account that has enabled the two-factor authentication. It gives an authentication failed error. For email accounts that have not enabled the two-factor authentication works fine. How to resolve this issue?
using (SmtpClient client = new SmtpClient())
{
client.Port = Convert.ToInt32(appSettings["Port"]);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Host = "smtp.office365.com";
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(SenderMailAddress, SenderMailPassword);
email.Subject = String.Format("{0}", txtMailSubject.Text);
//
email.Body = String.Format("{0}", text);
email.IsBodyHtml = true;
client.Send(email);
}
Error message is
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
You have to create an application password, then use the new application password in your code, check this link
MailAddress from = new MailAddress("fromid");
MailAddress to = new MailAddress("toID");
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = #"The sensor get offline ...";
SmtpClient client = new SmtpClient(server);
client.Host = "hostID";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential
{
UserName = "**********",
Password = "*************,
};
Console.WriteLine("Sending an email message to {0} using the SMTPhost {1}.");

.Net email integration won't send using godaddy SMTP Credential

Hi its been almost a day that I've been figuring things out with regards to sending email from godaddy email account to a gmail account. I have had my research online and almost tried everything but no luck .. here's what I made so far.
protected void generateEmail(){
MailMessage mail = new MailMessage ();
mail.From = new System.Net.Mail.MailAddress ("contact#company.com");
// The important part -- configuring the SMTP client
SmtpClient smtp = new SmtpClient ();
smtp.Port = 465; // [1] You can try with 465 also, I always used 587 and got success
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; // [2] Added this
smtp.UseDefaultCredentials = false; // [3] Changed this
smtp.Credentials = new NetworkCredential ("contact#company.com", "password123!"); // [4] Added this. Note, first parameter is Email Address of the sender and the next parameter is the password.
smtp.Host = "relay-hosting.secureserver.net";
//recipient address
mail.To.Add (new MailAddress ("test#gmail.com"));
mail.To.Add (new MailAddress ("testagain#gmail.com"));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "This is a Test Message";
mail.Body = st;
smtp.Send (mail);
}
Can anyone help me out ? would appreciate any hands..
protected void generateEmail()
{
//Create the msg object to be sent
MailMessage msg = new MailMessage();
//Add your email address to the recipients
msg.To.Add("whereEmailWillBeSent#gmail.com");
//Configure the address we are sending the mail from
MailAddress address = new MailAddress("mail#company.com");
msg.From = address;
msg.Subject ="Hi this is mail from company";
msg.Body = "Your Message";
SmtpClient client = new SmtpClient();
//for Godaddy
client.Host = "relay-hosting.secureserver.net";
client.Port = 25;
client.EnableSsl = false;
client.UseDefaultCredentials = false;
//Send the msg
client.Send(msg);
//Display some feedback to the user to let them know it was sent
}
}

How to send mail using SmtpClient in .net?

I have the following code which is not working:
public static void SendMail(string content, string title, List<string> address)
{
SmtpClient client = new SmtpClient(Server,Port);
client.Port = Port;
client.Host = Server;
client.EnableSsl = false;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(Username, Password);
foreach(string to in address)
{
MailMessage mm = new MailMessage(From, to, title, content);
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.Send(mm);
}
client.Dispose();
}
I am getting the following error:
Mailbox unavailable. The server response was: You must give your username and password to send mail through this service
You can see that I am passing a username and password. Why am I still getting this issue?
here i am using example of using gmail server
MailMessage mail = new MailMessage();
mail.To.Add(textBox1.Text);
mail.From = new MailAddress("Yourgmailid");
mail.Subject = "Email using Gmail";
string Body = "Hi, this mail is to test sending mail" +
"using Gmail in ASP.NET";
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
("Yourgmailid, "Password");
smtp.EnableSsl = true;
smtp.Send(mail);

Error While Sending Email Using GmailD

I am getting following error message while sending email using gmailD.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
MailMessage objMailMessage = new MailMessage();
objMailMessage.From = new MailAddress("suraj.podval#in.vsolutions.com");
objMailMessage.To.Add(new MailAddress("itslaxman#gmail.com"));
objMailMessage.Subject = "Test";
objMailMessage.Body = "Test Test";
objMailMessage.IsBodyHtml = true;
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("user#gmail.com", "password");
smtpClient.Send(objMailMessage);
Try changing the port to 465
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
// Your gmail email address
oMail.From = "gmailid#gmail.com";
// Set recipient email address
oMail.To = "support#emailarchitect.net";
// Set email subject
oMail.Subject = "test email from gmail account";
// Set email body
oMail.TextBody = "this is a test email sent from c# project with gmail.";
// Gmail SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// If you want to use direct SSL 465 port,
// please add this line, otherwise TLS will be used.
// oServer.Port = 465;
// detect SSL/TLS automatically
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
// Gmail user authentication
// For example: your email is "gmailid#gmail.com", then the user should be the same
oServer.User = "gmailid#gmail.com";
oServer.Password = "yourpassword";
check below link: http://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=2

Categories