How to send an email, via ExchangeService C# - c#

I have to send an email through Exchange. I found that I can do this using ExchangeService. I've tried many ways to do this, but only what I've got is error: (440) Login Timeout.
I don't even know if there is a problem with credentials, or my code is wrong.
string login = #"login";
string password = #"password";
ExchangeService service = new ExangeService();
service.Credentials = new NetworkCredential(login, password);
service.Url = new Uri(#"https://mail.exchangemail.com");
EmailMessage message = new EmailMessage(service);
message.Subject = "Interesting";
message.Body = "The proposition has been considered.";
message.ToRecipients.Add("quarkpol#gmail.com");
message.From = "quarkpol_test#gmail.com";
message.Send();

I've found the problrm solution on the web, and it works.
using EASendMail;
string login = #"login";
string password = #"password";
SmtpMail oMail = new SmtpMail("TryIt");
SmtpClient oSmtp = new SmtpClient();
oMail.From = "from#email.com";
oMail.To = "to#email.com";
oMail.AddAttachment(fileName, fileByteArray);
oMail.Subject = "Subject";
oMail.HtmlBody = "HTMLBody";
SmtpServer oServer = new SmtpServer("mail.email.com");
oServer.Protocol = ServerProtocol.ExchangeEWS;
oServer.User = login;
oServer.Password = password;
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
try
{
Console.WriteLine("start to send email ...");
oSmtp.SendMail(oServer, oMail);
Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}

Related

The smtp service is not working but there is no errors .net6

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.

how to write a simple code that sends email

i developed a simple website that stores in the user name and sends that to my emailid and then downloads a file.File is getting downloaded but not mailing me the username.
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("mygmailid");
mailMessage.From =new MailAddress("mydomainbasedemailid");
mailMessage.Subject = "ASP.NET e-mail test";
mailMessage.Body = "Hello world,\n\nThis is an ASP.NET test e-mail!";
SmtpClient smtpClient=new SmtpClient("mail.mydomain.com",587);
smtpClient.Send(mailMessage);
Response.Write("E-mail sent!");
}
catch (Exception ex)
{
Response.Write("Could not send the e-mail - error: " + ex.Message);
}
Try to set the Smtp.Credentials like this:
string HOSTLOGIN = "YourHostLogin";
string HOSTPW = "YourTopSecretPasswort";
var credentials =
new System.Net.NetworkCredential() { UserName = HOSTLOGIN, Password = HOSTPW };
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
client.EnableSsl = true;

SMTP server not getting credentials from C# code

I'm trying to set up an automatic email sender.
My smtp server requires authentication. I'm not using ssl but still I'm getting the following response from server:
The server response was: 5.7.1 You are not authorized, authentication is required.
And if I check on the server it got an empty username.
Here is my code:
public void email_send(string mailadress,string docname)
{
string host = "myhost";
string username = "user";
string password = "password";
int port = 625;
var nc = new NetworkCredential(username, password);
var auth = nc.GetCredential(host,port,"Basic");
using (var mail = new MailMessage())
using (var SmtpServer = new SmtpClient())
{
SmtpServer.Host = host;
SmtpServer.Port = port;
SmtpServer.Credentials = auth;
mail.From = new MailAddress("fromadress");
mail.To.Add(mailadress);
mail.Subject = "Subject";
mail.Body = "Body";
Attachment attachment;
attachment = new Attachment("D:/" + docname + ".pdf");
mail.Attachments.Add(attachment);
mail.IsBodyHtml = true;
SmtpServer.DeliveryFormat = SmtpDeliveryFormat.International;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
SmtpServer.Send(mail);
}
}

asp c# send email without configuring sender credentials

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

My C# code can't send mail but outlook 2k7 can

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);
}

Categories