Email id address issue in c# code - c#

I wrote a simple program in C# Winforms for sending an email and my code is mentioned below:-
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MailMessage rtnMail()
{
string to = txt_To.Text;
string from = txt_From.Text;
string subject = txt_Subject.Text;
string body = txt_Body.Text;
MailMessage message = new MailMessage(from, to, subject, body);
return message;
}
//Button click event
private void btn_Send_Click(object sender, EventArgs e)
{
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myanotherid#gmail.com", "password");
smtp.EnableSsl = true;
smtp.Timeout = 500000;
smtp.Send(this.rtnMail());
}
}
when i run this code and put all the values in textboxes like (to, from, body, subject) and click the "Send" button i do end up getting an email at an address
mentioned in the Textbox named txt_To ( which is my recipient gmail account id).But whenever i look at which address(email id) i got this email from in Microsoft
Outlook (which i have configued for my gmail recipeint account), it always says that i got this email from the email address mentioned as first argument in the line of
code below,
smtp.Credentials = new System.Net.NetworkCredential("myanotherid#gmail.com", "password");
My question is, am i doing anything wrong because i expect that email address from which im receiving an email( in my outlook gmail) should be the one that i put in
TextBox named txt_From rather than from "myanotherid#gmail.com" address.
Is there a work around or does there exist an alternate to it.

I guess it's gmail's protection to prevent sender spoofing.
You can't login to GMail as yogibear#gmail.com and send an e-mail as barack.obama#whitehouse.gov. GMail's SMTP will rewrite the message's header to properly indicate who has really sent the e-mail.

You should use new mailaddress();
MailAddress from = new MailAddress("someone#something.com", "John Doe");
MailAddress to = new MailAddress("someoneelse#something.com", "Jane Doe");
MailMessage mail = new MailMessage(from, to);
further reading here: http://msdn.microsoft.com/en-us/library/system.net.mail.mailaddress.aspx

Your code looks correct. Gmail does not allow you to specify a different 'from address' unless it is one you have proven belongs to you.
Go to Settings > Accounts > 'Send email as' and add an address there. You can only choose to send from any of the accounts you have configured here.

Related

Failed to send an EMail with body contains ip address and port no

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();

How can I change the "From" email address without a non-authenticated SMTP server?

All,
I'm writing an application that will allow customers to submit support tickets directly from their desktop. That being said, I'd like the "FROM" email address to be their email address.
I currently have the following code:
public void SendTicketEmail()
{
try
{
string tEmail = materialListView1.SelectedItems[0].SubItems[1].Text;
string tPhone = materialListView1.SelectedItems[0].SubItems[2].Text;
string tUser = materialListView1.SelectedItems[0].Text;
MailMessage mail = new MailMessage(tEmail, "my email");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.simplifymsp.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
I'm assuming that my resolution at this point is to request that my hosting service enable an option where I can allow smtp outgoing emails without authentication on a specified port?
The alternative here is to have all of the support emails from each customer come from one of my preset email addresses and include a Customer ID in the subject, then create a workflow in my helpdesk ticketing system for each customer that assigns the customer's information to that ticket. That's more work than I care to do, especially when scaling.
Thank you in advance.
For what it's worth, I resolved this issue rather simply. FreshService (the website that I use for my ticketing system) reacts well with the following code:
mail.From = new MailAddress("me#me.com", "customer#customer.com");
Even though the email is coming from "me#me.com," FreshService still reads the ticket as if it came from the customer. Works beautifully.
Thank you all for your time.

c# Send Email using Process.Start

I want to send simple email with no attachment using default email application.
I know it can be done using Process.Start, but I cannot get it to work. Here is what I have so far:
string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}", "to#user.com", "Subject of message", "This is a body of a message");
System.Diagnostics.Process.Start(mailto);
But it simply opens Outlook message with pre-written text. I want to directly send this without having user to manually click "Send" button. What am I missing?
Thank you
You need to do this :
string mailto = string.Format("mailto:{0}?Subject={1}&Body={2}", "to#user.com", "Subject of message", "This is a body of a message");
mailto = Uri.EscapeUriString(mailto);
System.Diagnostics.Process.Start(mailto);
I'm not sure about Process.Start() this will probably always only open a mail message in the default Mail-App and not send it automatically.
But there may be two alternatives:
Send directly via SmtpClient Class
using Outlook.Interop
You need to do this:
SmtpClient m_objSmtpServer = new SmtpClient();
MailMessage objMail = new MailMessage();
m_objSmtpServer.Host = "YOURHOSTNAME";
m_objSmtpServer.Port = YOUR PORT NOS;
objMail.From = new MailAddress(fromaddress);
objMail.To.Add("TOADDRESS");
objMail.Subject = subject;
objMail.Body = description;
m_objSmtpServer.Send(objMail);

Strings not compatible for email address

I am new to C#. In my verge of learning this language, I made a windows form application application that takes in an email address and a password and sends it to a pre-specified email address. I don't think that I have messed up the data types though I am getting an error The specified string is not in the form required for an e-mail address.
My code is below :
namespace mailTest
{
public partial class Form1 : Form
{
string mailAddress = "lancepreston#gmail.com";
string mailPassword = "123456789";
string SMTP = "smtp.gmail.com";
public Form1()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage(Email.Text, Password.Text); \\ I get the error on this line
SmtpClient client = new SmtpClient(SMTP);
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential(mailAddress,mailPassword);
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
}
}
}
Screenshot of my form :
MailMessage constructor is defined as below
public MailMessage(string from, string to);
First parameter is from address and second is to address, but you seem to pass password in second parameter. That's why you get the exception.
That particular constructor of the MailMessage class expects the first parameter to be the email address of the sender, and the second parameter to be the email address of the recipient:
http://msdn.microsoft.com/en-us/library/14k9fb7t(v=vs.110).aspx
You are providing a password to the parameter that expects the recipient's email address.
I presume you really want to pass the password in the body of the message.
Take a look at the constructor that populates the body, or set the Body property after initializing a MailMessage:
http://msdn.microsoft.com/en-us/library/5k0ddab0(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.body(v=vs.110).aspx
You have to specify UserName in NetworkCredential , like the following:
NetworkCredential myCredentials = new NetworkCredential("","","");
myCredentials.Domain = domain;
myCredentials.UserName = username;
myCredentials.Password = passwd;
See the explanation at:
http://msdn.microsoft.com/en-us/library/system.net.networkcredential.username%28v=vs.110%29.aspx
Also, your MailMessage has wrong parameters (shoud be from/to).
Regards,

From Address Shows NetworkCredential User Email

I have made an web application for sending e-mail. It works fine.
The problem is receiver end - Receiver shows NetworkCredential User Email as From Email.
And the email provided as From Email doesn't exist.
i want to show the suplied email not the networkcredential user email to the receiver.
sample code-
using System.Net.Mail;
MailMessage oMsg = new MailMessage();
oMsg.From = new MailAddress("sender#somewhere.com","Diplay Name");
oMsg.To.Add(new MailAddress("recipient#somewhere.com"));
oMsg.Subject = "Send Using Web Mail";
oMsg.Body ="Hi..";
System.Net.Mail.SmtpClient s = new System.Net.Mail.SmtpClient("host", port_no);
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("user", "password");
s.EnableSsl = true;
s.UseDefaultCredentials = false;
s.Credentials = nc;
s.Send(oMsg);
The receiver gets from email is "user" but i want to show "sender#somewhere.com".
I think you need to update the Display name of the e-mail address that you send from.
Update oMsg.From = new MailAddress("sender#somewhere.com"); to be oMsg.From = new MailAddress("sender#somewhere.com","sender#somewhere.com");
MailAddress has an overload which allows you to pass is a display name for the given mail addres e.g. new MailAddress("sender#somewhere.com", "Display Name");
Some mail services (such as google), override the .FROM value, and will always use the ENVELOPE value, which is the NetworkCredential username.
I have a feeling that is what you are seeing.

Categories

Resources