I have a program which is sending emails to my clients. The thing i wanna do is to hide the ip from which the email was send. Is this even possible if yes how? This is my code:
SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
MailMessage mail = new MailMessage(email, target, title, msg);
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential(email, password);
try
{
smtpClient.Send(mail);
}
catch (Exception e) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine($"Error occured while trying to send email:\n{e.Message}"); Console.ResetColor(); }
You're looking at the wrong side. Gmail already strips the IP address of the client. If you own an email server this can be configured as well.
Related
I wrote a simple application for windows to send some emails to members of my mailing list.
The program uses my gmail account to send via SMTP. I do not want my customers having that account however, I want them to see instead the corporate email.
I sent myself some emails to test, but the emails I am recieving are showing sent from the email account Ive logged in with at google. Any ideas?
The mail function:
public void SendEmail()
{
//smtp host and port for gmail
string host = txtHost.Text;
int port;
if (!Int32.TryParse(txtPort.Text, out port))
{
MessageBox.Show("Please enter a valid port number.");
return;
}
//compose email
MailMessage msg = new MailMessage();
msg.Sender = new MailAddress(txtFrom.Text, txtFrom.Text);
msg.From = new MailAddress(txtFrom.Text, txtFrom.Text);
msg.To.Add(txtTo.Text);
msg.Subject = txtSubject.Text;
msg.Body = rTxtMessage.Text;
msg.IsBodyHtml = chkHtml.Checked;
//msg.From = new MailAddress(txtFrom.Text, txtFrom.Text);
//create smtp client
SmtpClient smtp = new SmtpClient(host, port);
//TODO: Move constants to the NetworkCredentials call
string username = SMTP_USERNAME;
string password = SMTP_PASSWORD;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential(username, password);
smtp.EnableSsl = true;
try
{
//Send email
smtp.Send(msg);
}
catch (Exception exp)
{
//Log if any errors occur
MessageBox.Show(exp.Message);
}
}
And the application and result screenshots:
You should set:
msg.Sender
to your Company address.
What i tried now is to send email to my own gmail account but what ever i tried so far i'm getting error when trying to send.
I didn't try to send yet to my pop3 email and not to yahoo or from my pop3 or from yahoo. For now i'm trying to send to my gmail.
In a button click event:
private void button1_Click(object sender, EventArgs e)
{
sendit("myg#gmail.com");
}
Then in the method sendit
public string sendit(string ReciverMail)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("myg#gmail.com");
msg.To.Add(ReciverMail);
msg.Subject = "Hello world! " + DateTime.Now.ToString();
msg.Body = "hi to you ... :)";
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = true;
client.Host = "smtp.gmail.com";
client.Port = 587;
client.EnableSsl = true;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("myg#gmail.com", "myp");
client.EnableSsl = true;
client.Timeout = 20000;
try
{
client.Send(msg);
return "Mail has been successfully sent!";
}
catch (Exception ex)
{
return "Fail Has error" + ex.Message;
}
finally
{
msg.Dispose();
}
}
Using break point it's always getting to the catch part show the message:
"The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.5.1 Authentication Required.
Learn more at"
Remove client.UseDefaultCredentials = true; and try again!
If UseDefaultCredentials is set, the client tries to send the E-Mail with the currently logged on user. If UseDefaultCredentials is false, then it is going to use your manually set Credentials!
It is a security issue, Gmail by default prevents access for your mail account from custom applications. You need to turn on the access for less secure applications.
Here is the link: Less secure applications
The following is my code to send a mail over a http proxy. I have set my proxy to the same as mentioned. Now when I try to run the program, after some time it says *System.Net.WebException: The remote name could not be resolved. May I know where I am going wrong?
protected void mailto(string message)
{
WebRequest.DefaultWebProxy = new WebProxy("10.1.1.4", 8080);
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("mymail#gmail.com");
mail.To.Add("to_address");
mail.Subject = "For Data Using";
mail.Body = message;
SmtpServer.Port = 465;// Tried even with 587, but no luck
SmtpServer.Credentials = new System.Net.NetworkCredential("mymail#gmail.com", "mypassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Debug.WriteLine(Environment.NewLine + "message sent");
}
catch (Exception ex)
{
Debug.WriteLine("Message not sent" + ex.ToString());
}
}
I have faced same issue with sending a mail from c# using Gmail SMTP, the problem was the Port.
Try to use the port : 587
SmtpServer.Port = 587;
Also don't forget to Enable POP & IMAP Access to your Gmail account, go to settings , choose 'Forwarding and POP/IMAP' from the tabs and enable the POP & IMAP
I'm trying to figure out how to create a simple contact page in order to send an email. Right now I'm just testing but i'm not getting any error message.
Here is my code:
protected void btnSend_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.To.Add(txtFrom.Text);
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
SmtpClient smtp = new SmtpClient("localhost");
smtp.Send(msg);
Response.Write("Your email was sent");
}
Do you have an Smtp service running on your localhost? If not you can use Gmail's smtp service for testing.
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.Credentials = new NetworkCredential("<your gmail login>", "<your gmail password>");
client.EnableSsl = true;
As others have pointed out, you probably don't have the localhost as a real mail server. To get a better idea, turn Tracing on and change your code to something more like this:
protected void btnSend_Click(object sender, EventArgs e) {
Trace.Write("btnSend_Click initialized");
string resp = "An unknown error occured.";
using (MailMessage msg = new MailMessage()) {
try {
msg.To.Add(txtFrom.Text);
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
SmtpClient smtp = new SmtpClient("localhost");
Trace.Write("smtp client created.");
smtp.Send(msg);
Trace.Write("smtp message sent.");
resp = "Your message was sent.";
} catch (Exception ex) {
Trace.Warn("Smtp Error", ex.Message);
resp = "There was an error sending your message.";
}
}
Response.Write(resp);
Trace.Write("btnSend_Click completed");
}
You must specify a From address like this:
msg.From = new MailAddress("noreply#noreply"); // Use a real address, of course
Because you are not getting an exception, I wonder if btnSend_Click is getting hit at all...
Other things to look at:
Is your txtFrom.Text being overwritten on postback? You might hardcode your email address during debugging.
Try 127.0.0.1 instead of localhost.
I have my code here, it works fine from my home, where my user is administrator, and I am connected to internet via a cable network.
But, problem is when I try this code from my work place, it does not work. Shows error:
"unable to connect to the remote server"
From a different machine in the same network:
"A socket operation was attempted to an unreachable network 209.xxx.xx.52:25"
I checked with our network admin, and he assured me that all the mail ports are open [25,110, and other ports for gmail].
Then, I logged in with administrative privilege, there was a little improvement, it did not show any error, but the actual email was never received.
Please note that, the code was tested from development environment, visual studio 2005 and 2008.
Any suggestion will be much appreciated.
Thanks in advance
try
{
MailMessage mail_message = new MailMessage("xxxxx#y7mail.com", txtToEmail.Text, txtSubject.Text, txtBody.Text);
SmtpClient mail_client = new SmtpClient("SMTP.y7mail.com");
NetworkCredential Authentic = new NetworkCredential("xxxxx#y7mail.com", "xxxxx");
mail_client.UseDefaultCredentials = true;
mail_client.Credentials = Authentic;
mail_message.IsBodyHtml = true;
mail_message.Priority = MailPriority.High;
try
{
mail_client.Send(mail_message);
lblStatus.Text = "Mail Sent Successfully";
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
lblStatus.Text = "Mail Sending Failed\r\n" + ex.Message;
}
}
catch (Exception ex)
{
lblStatus.Text = "Mail Sending Failed\r\n" + ex.Message;
}
Here is some sample code that works for me and talks to a gmail server
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
Try setting UseDefaultCredentials to false.
Also try switching SSL on for y7mail
mail_client.EnableSSL = true;
Source - http://help.yahoo.com/l/au/yahoo7/edit/registration/registration-488246.html
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
This is the best answer for System.Exception Problem. Totally verified, you need to provide username and password at network credentials. Also try to give this solution to other sites.