I am trying to send out an email to a mailing list that I have created, but when I run the function it just times out. I am not sure whether it is a host error or if I'm missing code or what. I am running an mvc format program on a local server.
I originally didn't have the credentials, but adding them changed nothing. Both before and after adding them all that happens is it loads for about a minute before reporting a timeout.
private void SendEmail(string sender, string[] attachments, List<string> recipients, ReleaseNotes notes, string username, string password)
{
SmtpClient client = new SmtpClient();
MailMessage mail = new MailMessage();
mail.Subject = "Software Release of VCM Version " + notes.ReleaseVersion;
mail.From = new MailAddress(sender);
mail.Body = GetEmailBody(notes);
mail.IsBodyHtml = true;
foreach (string r in recipients)
{
mail.To.Add(r);
}
foreach (string a in attachments)
{
mail.Attachments.Add(new Attachment(a));
}
client.Host = "pod51213.outlook.com";
client.Credentials = new NetworkCredential(username, password);
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
}
I am supposed to see the email appear in my inbox while the screen switches over to a screen saying email was sent. Its not giving me an actual error code. It just says operation timed out.
I was able to get the host I needed and get the function to run. The company I wrote the code for had their own host server. All I did to fix the code was delete the line that included client.Credentials, because I did not need that, and in the quotes for client.Host I replaced "pod51213.outlook.com" with "smtp.irco.com", which is the mail server for the company I built the program for.
I Think you should specify the port number
e.g.
SmtpClient("smtp.gmail.com", 587) // This for gmail
Related
I have create function to send an email. This function was work successful on localhost but on server its failed without any exception. I know the problem comes from my Port on IP Address.
The sample body is string body = "<p>Please click here</p>Thank You."
The problem is : between IP Address and Port.
Successful send an email if i remove :.
Do you guys have any ideas?
public void Sent(string sender, string receiver, string subject, string body)
{
using (MailMessage mail = new MailMessage(sender, receiver))
{
using (SmtpClient client = new SmtpClient())
{
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "mail.companyName.com.my";
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = body;
client.Send(mail);
}
}
}
You are doing it right, the code to send the mail is ok (you may want to revise the function name and make the smtp host name configurable, but that is not the point here).
The e-mail delivery fails on a relay, there is no immedieate feedback (no exception) to the client about this kind of failure.
The best bet is the IncreaseScoreWithRedirectToOtherPort property set in Set-HostedContentFilterPolicy in case your mail provider is Office365, or a similar spam filter mechanism in any other mail provider that is encountered down the mail delivery chain.
You can set a reply-to address and hope that the destination server will bounce a delivery failure that gives you more information. Or have the admin of the mail server look up the logs. More information here:
https://serverfault.com/questions/659861/office-365-exchange-online-any-way-to-block-false-url-spam
Try setting the 'mail.Body' to receive a Raw Html message instead of a encoded string, like:
mail.Body = new System.Web.Mvc.HtmlHelper(new System.Web.Mvc.ViewContext(), new System.Web.Mvc.ViewPage()).Raw(body).ToString();
Or put a using System.Web.Mvc at the beginning so it gets shorter and easier to understand:
using System.Web.Mvc
mail.Body = new HtmlHelper(new ViewContext(), new ViewPage()).Raw(body).ToString();
I have a web application using ASP.net and C#,in one step it will need
from the user to
send an email to someone with an attachments.
my problem is when the user will send the email i don't want to put their
password every time the user send.
i want to send an email without the password of the sender.
any way to do that using SMTP ?
and this is a sample of my code "not all".
the code is worked correctly when i put my password , but without it ,it
is not work, i need a way to send emails without put the password but
in the same time using smtp protocol.
private void button1_Click(object sender, EventArgs e)
{
string smtpAddress = "smtp.office365.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "my email";
string password = "******";
string emailTo = "receiver mail";
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.UseDefaultCredentials = true;
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
MessageBox.Show("message sent");
}
}
I believe this can be accomplished easily, but with some restrictions.
Have a look at the MSDN article on configuring SMTP in your config file.
If your SMTP server allows it, your email object's from address may not need to be the same as the credentials used to connect to the SMTP server.
So, set the from address of your email object as you already are:
mail.From = new MailAddress(emailFrom);
But, configure your smtp connection one of two ways:
Set your app to run under an account that has permission to access the SMTP server
Include credentials for the SMTP server in your config, like this.
Then, just do something like this:
using (SmtpClient smtp = new SmtpClient())
{
smtp.Send(mail);
}
Let the configuration file handle setting up SMTP for you. This is also great because you don't need to change any of your code if you switch servers.
Just remember to be careful with any sensitive settings in your config file! (AKA, don't check them into a public github repo)
I'm trying to send email from asp.net mvc controller. Gmail account used here for smpt is configured to use with less security, so that's not the problem here.
but I don't get any error message neither any exception, but it not
deliver at my expected email address.
I'm using code
var text = "email body to deliver";
SendEmail("mydeliverEmailAddress#gmail.com", text);
public static bool SendEmail(string SentTo, string Text)
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("myemail#gmail.com", "myGmailPass");
client.Port = 465;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
MailAddress
maFrom = new MailAddress("sender_email#domain.tld", "Sender's Name", Encoding.UTF8),
maTo = new MailAddress(SentTo, "Recipient's Name", Encoding.UTF8);
MailMessage mmsg = new MailMessage(maFrom.Address, maTo.Address);
mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
mmsg.IsBodyHtml = true;
mmsg.Subject = "Some Other Text as Subject";
mmsg.SubjectEncoding = Encoding.UTF8;
client.Send(mmsg);
}
catch (Exception ex)
{
}
return true;
}
Wait a minute. You are using your gmail account: myemail#gmail.com and trying to send an email on behalf of sender_email#domain.tld?
For more than obvious reasons that's never gonna work. So make sure that you are using the same email address as the one you are authenticating against:
maFrom = new MailAddress("myemail#gmail.com", "Sender's Name", Encoding.UTF8),
You can only send emails from the account you are authenticated against. Of course the recipient email can be any address that gmail can deliver to.
You've got another issue with your code. You are using a wring port here:
client.Port = 465;
The correct port that gmail SMTP works with is the following:
client.Port = 587;
Also you might want to ensure that you have enabled less secure apps in your gmail account or you will not be able to use SmtpClient in .NET to send emails using this SMTP: https://www.google.com/settings/security/lesssecureapps?pli=1
but I don't get any error message neither any exception, but it not
deliver at my expected email address.
What error message do you expect to get when you did the worst ever possible thing? You wrapped your code in a try/catch block and in your catch block you did absolutely nothing. You just consumed the exception:
catch (Exception ex)
{
}
So make sure that you do something useful with an exception if you are going to be catching it. For example something useful could be to log this exception and send an error message to the user saying that something bad happened and you couldn't send an email and that you are investigating the issue right now.
var smtpClient = new SmtpClient("YourSMTPServer", "SMTPServerPort"))
{
Credentials = new NetworkCredential("YourEmail",
"Password"),
EnableSsl = false
};
string fromEmail = "YourEmail";
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromEmail);
mailMessage.To.Add("Recipient's EMail");
mailMessage.Subject = "Test Mail";
mailMessage.Body = "This is test Mail";
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);
Today I am trying to create a unit test in visual studio for sending a email i am struggling with finding the best way to do it and using SendCompleted event as a validation for message been actually send.
Here i code i apply to send email but after using many different ways i just paste here clean code and maybe you can tell me what i am doing wrong or give me better way to solve it.
Here is my code what i have tried so far :
[TestClass]
public class sendEmailTest
{
[TestMethod]
public void sendAsyncEmailTest()
{
string from = "sender#test.com";
string to = "receiver#test.com";
MailMessage mail = new MailMessage(from, to);
mail.Subject = "Unit Test MVC";
mail.Body = "Unit Test for sending mail in MVC app";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.test.com";
smtp.EnableSsl = true;
NetworkCredential networkCredential = new NetworkCredential(from, "testpassword");
smtp.UseDefaultCredentials = true;
smtp.Credentials = networkCredential;
smtp.Port = 587;
smtp.SendAsync(mail, null);
smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
}
static void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
}
}
What you probably need is dummy dev SMTP server.
You can get it from here
Once it's running on your dev machine you can send emails setting smtp.Host to 'localhost' and using dummy email addresses.
However as mentioned in comments such unit test isn't useful at all as whether email is sent successful or not depends on many external factors like e.g network connection. If you past test on your local machine it does not mean sending email going to be successful every time on production server.
I think i need some guru lights!
public void SendEndingMail(string fileName)
{
SmtpClient client;
client = new SmtpClient("smtp.myserver.com", 25);
//client = new SmtpClient();
if (!string.IsNullOrEmpty(""))
{
System.Net.NetworkCredential credential = new NetworkCredential("", "");
client.Credentials = credential;
}
client.UseDefaultCredentials = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
MailAddress fromAddress = new MailAddress("mailing#mydom.com", "Elec");
MailAddress toAdrress = new MailAddress("mailing#mydom.com");
using (System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(fromAddress, toAdrress))
{
mailMessage.Attachments.Add(new System.Net.Mail.Attachment(fileName));
mailMessage.IsBodyHtml = false;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
try
{
client.Send(mailMessage);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
Is that true that:
when i set
client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
It does not matter whichever smtp server i use
client = new SmtpClient("smtp.myserver.com", 25);
//client = new SmtpClient();
The both lines are the same since it will use LOCAL IIS ?!!!
Is this is true, it is not normal that the API is build this way!? it is very confusing...
Thanks
Jonathan
IIRC, when the SmtpClient sends the email, it looks at the .DeliveryMethod value. If the value is Network, then it sends via network. If it is PickupDirectoryFromIis, then it ignores any specified SMTP server (because it just writes and the email to the filesystem), and writes it to the Pickup directory. No network communication takes place.
That is a bug in the Send routine - it creates an smtp server object even if one isn't specified, and when it later (after Send) tries to dispose it, it throws an exception. This happens AFTER the mail is successfully placed in the pickup directory, so the mail will be sent.
Workarounds:
Specify localhost as SMTP server. It won't be used, but prevents the exception.
A blind try/catch around the Send method (BAD solution).