I have functionality in my site which sends a email to an id .. Email contains a link to a webpage and a security key .. But the problem is it goes into the junk folder
I'm using free hosting by Somee.com
Code:
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Body = ("Copy The Link And paste It In Them follow Link Download </br>"+ encoded_url);
message.From = new MailAddress("lz-wag#hotmail.com");
message.To.Add(TextBox2.Text);
message.Subject = user + " Has Share The File With You";
try{
SmtpClient client = new SmtpClient();
client.Host = "smtp.live.com";
client.EnableSsl = true;
System.Net.NetworkCredential networkcred = new System.Net.NetworkCredential();
networkcred.UserName = "lz-wag#hotmail.com";
networkcred.Password = "password";
client.Port = 587;
client.Credentials = networkcred;
client.Send(message);
sendFile.Visible = false;
Label1.Visible = true;
Label1.Text = "Your File Has Been Shared";
}
catch(Exception ex){
Label1.Visible = true;
Label1.Text = "Your File Is Not Shared";
//Label1.Text = ex.ToString(); ;
}
Whether or not the email goes into the junk mail folder is a function of the email client, not a function of how you are sending the email.
However, FYI, both the MailMessage and the SmtpClient implement IDisposable, so should be in using blocks. Something like this:
using (MailMessage message = new MailMessage())
{
// ...
using (SmtpClient client = new SmtpClient())
{
// ...
client.Send(message);
}
}
I also suggest that you log the exception somewhere, or you'll never know what went wrong when something goes wrong.
Related
Sometimes following code sends Email,Sometimes i get SMTP Exception saying "failure in sending Email".Why is this happening? following is the code i have written.Plesae tell me why sometime i get email sent and sometime why i get Exception.
StringBuilder mailBody = new StringBuilder();
mailBody.Append("Hi <br/>")
MailMessage message = new MailMessage("from#gmail.com", "to#gmail.com");
message.Subject = "Subject";
message.Body = mailBody.ToString();
SmtpClient smtpclient = new SmtpClient("smtp.gmail.com", 587);
smtpclient.Credentials = new System.Net.NetworkCredential()
{ UserName = "from#gmail.com",
Password = "password"
};
smtpclient.EnableSsl = true;
message.IsBodyHtml = true;
smtpclient.Send(message);
When i send email there is an exception which says "failure sending mail".
var mailObj = new MailMessage("marabifuad2013#gmail.com", "to Email");
mailObj.Subject = "subject";
mailObj.Body ="";
mailObj.IsBodyHtml = true;
var smtpServer = new SmtpClient("smtp.gmail.com", 587);
smtpServer.Credentials = new NetworkCredential("marabifuad2013#gmail.com", "password");
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpServer.Send(mailObj);
I guess that because you haven't specified to use SSL:
smtpServer.EnableSsl = true;
Also you can try:
smtpServer.UseDefaultCredentials = true;
Apart from that everything looks fine, providing that you entering correct credentials and etc.
I am trying to attach a excel file with mail, but not getting where I am doing the mistake. There is no error coming but mail does not get send. When I try to check the server path of file with file exist. it show me true, so my file path is correct.
Mail is going properly without attachment.
MailMessage Msg = new MailMessage();
Msg.Subject = "User Creation";
string body = MailSending.PrepareMailBodyWith("WelcomeTemplate.htm", "LoginID", LoginID, "password", password);
string clientCoverage = System.Web.HttpContext.Current.Server.MapPath("~/file/abc.xlsx");
bool isFile = false;
if (File.Exists(clientCoverage))
{
isFile = true;
}
byte[] bytes = System.IO.File.ReadAllBytes(clientCoverage);
MemoryStream stream = new MemoryStream(bytes);
lblerror.Text = avnTutorial + isFile.ToString() ;
Attachment attachment = new Attachment(stream, "document.xlsx");
Msg.Attachments.Add(attachment);
Msg.Body = body;
Msg.IsBodyHtml = true;
Msg.From = new MailAddress("info#xyz.in");
Msg.To.Add(new MailAddress(Email));
SmtpClient client = new SmtpClient();
client.Host = "mail.xyz.in";
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("info#xyz.in", "xyz");
client.Send(Msg);
I tried with without memory stream also here is the code but does not work.
Attachment attachment = new Attachment(clientCoverage, MediaTypeNames.Application.Octet);
Here I am in a strange situation. When I send the email from localhost it is working fine but when I upload the page to the server and try to send email, I get the following error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
I'm displaying the error message in a label for testing purpose and the try is also misplaced I know, I will set it later.
the code I am using is
if (Page.IsValid)
{
try
{
StringBuilder message = new StringBuilder();
message.Append("Hello My Name is ");
message.Append(txtName.Text);
message.AppendLine();
message.AppendLine("My Contact Number " + txtContactNumber.Text.ToString());
message.AppendLine();
message.AppendLine();
message.AppendLine("My Email Id Is " + txtFromEmailAddress.Text.ToString());
message.AppendLine();
message.Append(txtEmailBody.Text);
MailMessage mailMessage = new MailMessage("xxx#gmail.com", "yyy#gmail.com");
mailMessage.Subject = "New Client Query";
mailMessage.Body = message.ToString();
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 25);
//smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "xxx#gmail.com",
Password = "password"
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
txtContactNumber.Text = "";
txtFromEmailAddress.Text = "";
txtName.Text = "";
txtEmailBody.Text = "";
lblEmailStatus.Text = "Email Sent Successfully.";
lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
}
catch(Exception ex)
{
lblEmailStatus.Text = ex.Message + " <br> " + ex.Source;
}
}
else
{
lblEmailStatus.Text = "Error! Email Not Sent ";
lblEmailStatus.ForeColor = System.Drawing.Color.Yellow;
}
I have googled for a couple of hours and checked links at this site as well but I still cant figure it out.
Now I request you guys here if any one have any solutions / hint.
Try this
public string SendEmailTest(String EmailMessage, String FromMail, String MailPassword, String MailServer, String To, String CC, String BCC, String DisplayName, String Subject, String Attachment)
{
try
{
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress;
fromAddress = new MailAddress(FromMail);
smtpClient.Host = MailServer;
smtpClient.Port = 25;
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(FromMail, MailPassword);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = SMTPUserInfo;
message.From = fromAddress;
message.To.Add(new MailAddress(To, DisplayName));
if (CC != "")
message.CC.Add(new MailAddress(CC, DisplayName));
if (BCC != "")
message.Bcc.Add(new MailAddress(BCC, DisplayName));
message.Subject = Subject;
message.IsBodyHtml = true;
message.Body = EmailMessage;
if (Attachment != "")
message.Attachments.Add(new Attachment(Attachment));
message.Priority = MailPriority.High;
smtpClient.Send(message);
return "SendEmail";
}
catch (Exception ex)
{
return "Email :" + ex;
}
}
i got the reason finally .
the email i was sending emails from was kind of hacked some days before and for the security reasons google team had kind of marked my email as unsecure . i changed the emails address and it is working fine thanks you all.
Based on the Google Gmail Documentation it would appear that the Port should be 587 not 25. I found a few other questions that seem to be related here and here.
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
How to send e-mail on ASP .Net using outlook address??
I've tried this code but no luck:
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))
mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)
smtpClient.Send(mailMessage) //--> got error here
But while I'm execute the code, it got this error message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
I've tried to add a line of code:
smtpClient.Credentials = New System.Net.NetworkCredential(username, password)
But still can't send the e-mail.
Anyone can help?
Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
Try to set smtpClient.EnableSsl to true / false depending on your environment
I'm guessing you are using Exchange 2007 or later as backend?
Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.
Talk to your IT guys what they prefer.
I did it using c#.This may help you.Please check.
MailMessage msg1 = new MailMessage();
msg1.From = strEFrom;
msg1.To = strETo;
msg1.Cc = strECC;
msg1.Subject = "Hi";
msg1.Priority = MailPriority.High;
msg1.BodyFormat = MailFormat.Html;
msg1.Body = strBody;
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
SmtpMail.Send(msg1);
and in web.config file
<appSettings>
<add key="MailServer" value=""/>
</appSettings>
Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.
When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..
well try this may it will solve your problem..
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
if it does not solve your problem then check your mail server/ client for the problem.
public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
I use this code to send mail from my gmail account.
was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.