c# Sending email though smtp mail format error happens - c#

Hello I am using visual c# 2010 and I am trying to send an email with gmail but I get an exception when I try to send the email at:
MailMessage mail = new MailMessage(From.Text, To.Text, Subject.Text , richTextBox2.Text);
From.Text, To.Text, Subject.Text, richTextBox2.Text are all text boxes with the information inside.
Here is the whole mail segment I am using:
SmtpClient client = new SmtpClient(smtp.Text);
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential(Username.Text, Password.Text);
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("mail sent");
The exception is: FormatException was Unhanded
The exception description is :The specified string is not in the form required for an e-mail address
And I have tried just filling the information out like so :
MailMessage mail = new MailMessage("you#gmail.com", "me#gmail.com", "hello" , body.Text);
//example //example
But I still get an exception. What am I doing wrong ?
Here are the values :
To.Text = "gerardcrafting#gmail.com";
smtp.Text = "smtp.gmail.com";
Password.Text = "Password";
Username.Text = "staff.gerardcrafting.gmail.com";
Subject.Text = textBox1.Text + "Banned" + richTextBox4.Text;

Per the MSDN documentation for MailMessage Constructor (String, String, String, String), when a FormatException happens with this constructor the reason is:
from or to is malformed.
You stated that your From.Text is taff.gerardcrafting.gmail.com, that is not a valid email address, because it is missing the # symbol.

Change the input of Username.Text to simply "gerardcrafting".

Related

How do I send an Email using c#?

I am trying to make a Windows Form that sends an email when the user clicks a button, but every time I try, an exception is being raised: Failure sending mail. What is wrong with my code?
private void button1_Click(object sender, EventArgs e)
{
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("username", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("myemailadress#gmail.com");
smtpClient.Host = "smpt.gmail.com";
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>your message body</h1>";
message.To.Add("towhomisend#yahoo.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
MessageBox.Show(ex.Message);
}
}
use "smtp.gmail.com" instead of "smpt.gmail.com"
1) Check whether SMTP settings are correct and server is configured correctly (Host & Port Settings)
2)Check whether credentials(user name and password) are correct
3)Check whether firewall is blocking the request
4).Check port 587 If it is blocked in firewall
Port 587:
This is the default mail submission port. When a mail client or server is submitting an email to be routed by a proper mail server, it should always use this port.
The 'smtpClient.Host = "smpt.gmail.com";' part is incorrect.
Change "smpt.gmail.com" to "smtp.gmail.com"
"S M T P" - not - "S M P T"
use "smtp.gmail.com" instead of "smpt.gmail.com"
That aside
Confirm one more thing, You have to enable email from other program in your gmail.
When you try to send once, you will get a notification in you email ID.
You have to enable it there.

Specified string is not in the form required for an e-mail address

I'm using a table (Table has the location and mail id's) to get the e-mail id based on which location we are selecting.
Trying to send the mail gives the following error:
The specified string is not in the form required for an e-mail address.
Code:
public void sendmail()
{
SqlCommand cmd = new SqlCommand("select EmailID from Table where Location='"+Location.Text+"'",con);
cmd.ExecuteNonQuery();
string EmpMail = "Test#gmail.com";
string EmpMailTo = "cmd";
MailMessage message = new MailMessage(EmpMail, EmpMailTo);
SmtpClient client = new SmtpClient();
message.Subject = "Auto generated mail";
message.IsBodyHtml = true;
message.Body = "Test mail for auto generation";
client.Host = "Apprelay";
client.Send(message);
}
Can anyone please help me to fix this? Thanks in advance.
string EmpMailTo = "cmd";
I'm pretty sure cmd is not a valid email address.
My psychic debugging skills tell me that it's on this line:
MailMessage message = new MailMessage(EmpMail, EmpMailTo);
...and that it's a FormatException based on the documentation.
Try making EmpMailTo a valid email address.

Sending hardcoded email with attachment

I am trying to write a code for sending hard coded email with attachment i-e I don't want to use the buttons and text fields. I want when the program runs it should automatically go to location in my drive and attach some files and email it to the email address which I have already told that program while coding.
The normal code with buttons and text fields does not work. See below the normal code
MailMessage mail = new MailMessage(from.Text, to.Text, subject.Text, body.Text);
mail.Attachments.Add(new Attachment(attachment1.Text));
SmtpClient client = new SmtpClient(smtp.Text);
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential(username.Text, password.Text);
client.EnableSsl = true;
client.Send(mail);
MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
I have tried replacing from.Text, to.Text, subject.Text, body.Text and attachment1.Text with a string as
string from="abc#gmail.com";
string attachment1=#"c:\image1.jpg";
They give me errors.
Remove the .Text after each variable, as strings don't have a Text property.
Like this:
MailMessage mail = new MailMessage(from, to, subject, body);

asp.net email SmtpMail.SmtpServer.Insert(0, "");

I have a asp.net page that send an email to me.
SmtpMail.SmtpServer.Insert(0, ""); work fine.
What does it mean ? when I change it to SmtpMail.SmtpServer = "127.0.0.1";, it fails.
When I say SmtpMail.SmtpServer.Insert(0, ""), what I am exactly setting as my SMTP server ?
Actually
SmtpMail.SmtpServer.Insert(0, "");
does nothing.
SmtpServer property is of type String so you are basically calling string.Insert(int, string) which does not affect the string that you are calling insert on but returns a new instance of string with the with what you are trying to insert.
SmtpMail.SmtpServer = "google.com";
SmtpMail.SmtpServer = SmtpMail.SmtpServer.Insert(0, "mail.");
// now SmtpMail.SmtpServer will be "mail.google.com"
Just so you know, SmtpServer is obsolete. You should use SmtpClient instead: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
SmtpClient client = new SmtpClient();
//...
MailMessage message = new MailMessage(from, to);
// setup mail properties...
client.Send(message);

Sending Email through .NET code

I'm unable to send an email to yahoo server as my code is throwing exception as 'Failure Sending mail' in C# 2008.
Please provide SMTP HostName, PortName for yahoo server and gmail server.
And also kindly provide a good working C# code with which i can send an email directly to any of the mail servers.
Please provide complete working code...so that i will copy into Visual studio environment and execute the same.
As i'm getting exception since morning....unable to resolve the issue.
Kindly help me in this regard.
For Gmail:
var client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.Credentials = new NetworkCredential("youraccount#gmail.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#gmail.com");
mail.To.Add("youraccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
For Yahoo:
var client = new SmtpClient("smtp.mail.yahoo.com", 587);
client.Credentials = new NetworkCredential("youraccount#yahoo.com", "secret");
var mail = new MailMessage();
mail.From = new MailAddress("youraccount#yahoo.com");
mail.To.Add("destaccount#gmail.com");
mail.Subject = "Test mail";
mail.Body = "test body";
client.Send(mail);
Bear in mind that some ISPs (including mine) force their clients to use their SMTP server (as a relay). Spamming protection is the reason.
So you should avoid sending e-mail to the Internet from a client application, unless you give user a chance to specify his SMTP hostname or your app relies on the user's e-mail software (MAPI,...).
Imagine how much easier it would be for us to help you, if you posted the complete exception message, along with a stack trace.
Also, go one step farther and enable logging for System.Net.Mail, so we can see any possible failures at the network level.
If you don't know how to enable logging for SNM, here is a link:
http://systemnetmail.com/faq/4.10.aspx
Thanks!
Dave
There are two ways in which We can send the mail,
1)First is using javascript link "mailTo".This will not send the mail automatically but it will just open the mail window.Refer to the below code
<a class="label" onclick='javascript:buildEmail(this)'>Send Mail</a>
Find below the js method
function buildEmail(el) {
var emailId = Usermail#gmail.com;
var subject="Hi";
var body="Hello";
el.href = "mailto:" + emailId + "?Subject=" + escape(subject) +
"&Body=" + escape(body);
}
2)The second way is to use System.Net.Mail which will automatically send the mail to the recipients in the secured manner.
string subject="Hello";
string body="Data";
using ( MailMessage objMail = new MailMessage ( "Yourmail#gmail.com", "Usermail#gmail.com" ) )//From and To address respectively
{
objMail.IsBodyHtml = false;// Message format is plain text
objMail.Priority = MailPriority.High;// Mail Priority = High
objMail.Body = "Hello";
ArrayList CCarr = new ArrayList();//Assume we add recipients here
// populate additional recipients if specified
if ( ( CCarr != null ) && ( CCarr .Count > 0 ) )
{
foreach ( string recipient in CCarr )
{
if ( recipient != "Please update the email address" )
{
objMail.CC.Add ( new MailAddress ( recipient ) );
}
}
}
// Set the subject of the message - and make sure it is CIS Compliant
if ( !subject.StartsWith ( "SigabaSecure:" ) )
{
subject = "SigabaSecure: " + subject;
}
objMail.Subject = subject;
// setup credentials for the smpthost
string username = "Username";
string passwd = "xxxxxx";
string smtpHost = "mail.bankofamerica.com";
SmtpClient ss = new SmtpClient ();
ss.EnableSsl= true;
ss.Host = smtpHost;
ss.Credentials = new NetworkCredential ( username, passwd );
ss.Send ( objMail );
}
Sigaba Secure Email secures e-mail from client to client through the use of desktop plug-ins and Web-based authentication and decryption.

Categories