I am using System.Net.Mail.SmtpClient to send mail in my Xamarin Form app. It's set to using my gmail address, and working great.
I would like to get the error from the smtp server (if there is one) to inform the user.
So, I am using the event _SendCompleted
Here my code
(sending email)
MailMessage mail = new MailMessage();
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress(outils.emailEmetteur);
mail.To.Add("toto#outlook.frfr");//Tiers.contactEmail);
mail.Subject = outils.emailObjetDefaut;
mail.Body = outils.emailMessageDefaut;
bool fileExist = File.Exists(filePath);
if (fileExist)
{
Attachment pJ = new Attachment(filePath);
mail.Attachments.Add(pJ);
smtpServer.Port = 587;
smtpServer.Host = "smtp.gmail.com";
smtpServer.EnableSsl = true;
smtpServer.UseDefaultCredentials = false;
smtpServer.Credentials = new NetworkCredential(outils.emailEmetteur, outils.emailEmetteurMDP);
try
{
smtpServer.SendAsync(mail, null);
smtpServer.SendCompleted += smtpServer_SendCompleted;
return true;
}
catch (Exception ex)
{
}
}
(send completed event)
private static void smtpServer_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
string token = (string)e.UserState;
string info = "";
if (e.Cancelled)
{
info = "Send canceled.";
}
if (e.Error != null)
{
info = e.Error.ToString();
}
else
{
info = "Message sent.";
}
}
I am trying to send an email to an incorrect address (toto#outlook.frfr)
My event is correctly triggered, but e.Cancelled and e.Error are NULL, and, in my Gmail inbox, I receive an error from smtp server telling me that the email address was incorrect, and that what I want to get in my Xamarin app.
Do you have any idea ? Thanks :)
Your client relays it to Gmail's outgoing mail server; the transaction to submit the message to SMTP is successful. The error only happens later, when Gmail tries to connect to the recipient's mail server, and can't resolve it; at that point, your client is no longer necessarily connected, and so the mail server generates a bounce message and delivers it to the sender's inbox.
In the general case, you either have to write your own mail server (but no, don't go there) or examine the inbox for bounce messages.
Related
I have an exchange account. I need to send an email with it. I tried sending with SmtpClient and it fails. I have few questions after googling
What are the things needed to send email with an exchange account?
What's difference between WebDav and MAPI? Which should be used?
Any Ideas?
Here is my code. I think this can't be done with SMTP.
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("sender#domain.com", "Sender");
mailMessage.To.Add("receiver#domain.com");
mailMessage.Subject = "Test Exchange Email";
mailMessage.Body = "Sameple test message";
SmtpClient smtp = new SmtpClient("Exchange Server Url");
smtp.EnableSsl = true;
smtp.Port = 465;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("username", "pwd");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mailMessage);
We want to send email using from email address instead of smtp email address...
I tried to send mail where from email address and smtp authenticated email address are different.
It gives me error.
You can based on the following code below. Hope it helps
MailMessage mailMessage = new MailMessage();
mailMessage.IsBodyHtml = true;
mailMessage.From = new MailAddress("sender#domain.com", "Subject"); // You can try changing this to the email address you want
mailMessage.ReplyToList.Add("sender#domain.com"); // Here you can add reply to
mailMessage.Subject = "ENQUIRY - " + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss");
mailMessage.Body = ""; // The body of email
SmtpClient smtpClient = new SmtpClient("mail.company-domain.com");
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Port = 587; // We used this port instead of port 25
smtpClient.Send(mailMessage);
When sending an email from C# with body being large is resulting in failure in sending email
Mailbox unavailable.
The email is working fine with a smaller body. I am using html body to true property..
Thanks,
Zafar
Code:
using (MailMessage _mailMsg = new MailMessage())
{
_mailMsg.From = new MailAddress(ConfigurationManager.AppSettings["mailFrom"].ToString());
_mailMsg.Body = mail.Body;
_mailMsg.Subject = mail.Subject;
_mailMsg.IsBodyHtml = true;
foreach (string strEmailIds in mailTo)
{
if (strEmailIds != null && strEmailIds != string.Empty && strEmailIds != "")
{
if (!_mailMsg.To.Contains(new MailAddress(strEmailIds)))
_mailMsg.To.Add(new MailAddress(strEmailIds));
}
}
//_mailMsg.CC.Add(ConfigurationManager.AppSettings["mailCC"].ToString());
using (SmtpClient _client = new SmtpClient(ConfigurationManager.AppSettings["Host"].ToString()))
{
if (_mailMsg.To.Count > 0)
{
_client.Send(_mailMsg);
}
else
{
_mailMsg.Subject = "No emails associated with the portfolio: " + account + " Original Email:" + mail.Subject;
_mailMsg.To.Add(new MailAddress(ConfigurationManager.AppSettings["mailSuppotTeam"].ToString()));
_client.Send(_mailMsg);
}
Oke, on thing it could be is that the mail server rejects big messages. Let exclude that one... I assume you have a local smtp mail server installed (check for telnet 127.0.0.1 25 that should give a sort of reply) configure the mail server [ConfigurationManager.AppSettings["Host"]] for 127.0.0.1, can you send big mails now?
If ConfigurationManager.AppSettings["Host"] is already the local SMTP server then:
a) stop that smtp service (Simple Mail Transfer Protocol) for a moment (via the command services.msc)
b) send a small email
c) go to c:\inetpub\mailroot\pickup and edit the message via notepad so that it becomes a BIG email
d) start the smtp service again (services.msc)
The issue was with sending email to cross domain email id and was resulting in the Generic exception
"Mailbox unavailable." May be this is one of the reason behind the above exception.
I am trying to send mail using SmtpClient and below is my code.
SmtpClient client_ = new SmtpClient("relay-hosting.secureserver.net", 25);
//client_.DeliveryMethod = SmtpDeliveryMethod.Network;
//client_.EnableSsl = true;
// client_.UseDefaultCredentials = false;
//client_.Credentials = new System.Net.NetworkCredential(_fromAddress, _password);
MailAddress from_ = new MailAddress(_fromAddress, _fromName);
MailMessage msg_ = new MailMessage(from_, from_);
msg_.Subject = "Subject";
StringBuilder body_ = new StringBuilder();
body_.AppendLine("Line1");
body_.AppendLine("===============================================================================================");
body_.AppendLine("Line2");
body_.AppendLine("===============================================================================================");
body_.AppendLine("line2");
body_.AppendLine("===============================================================================================");
msg_.Body = body_.ToString();
msg_.IsBodyHtml = true;
client_.Send(msg_);
I am getting TimeOut error. When Same smtp configuration using in email client on my machine, it send the mail immediately. I don't know what can be cause. Also when I used my Gmail account with gmail smpt server it worked.
Most consumer ISPs block port 25 to prevent you from running a mail server.
Therefore, you cannot connect to port 25 from your house.
You can ask them for an alternate port.
Try port 587; it's commonly used as well.
I wrote a WCF service for sending mail in Silverlight:
using System.Web.Mail;
MailMessage msg = new MailMessage();
msg.From = emailFrom;
msg.To = emailTo;
msg.Subject = msgSubject;
msg.Body = msgBody;
msg.Priority = MailPriority.High;
SmtpMail.Send(msg);
success = true;
This works fine in localhost, but when I host it in IIS, it doesn't show any error, but no mail has been received. What may be the problem?
Try to set the smtpserver property for the smtpmail class before you call the send method.