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);
Related
I am trying to send a group email depending on emails gathered from a select query called emailresults.
This is the code I have got so far but I am receiving error:
Could not send the e-mail - error: The specified string is not in the form required for an e-mail address
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
message.To.Add(emailresult);
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
If emailResults is some kind of Enumerable you will need to add each email address string from the Enumerable to the MailMessage.To MailAddressCollection. It's hard to say exactly how to do this without knowing the Type of emailresults. I expect you need something along these lines.
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
for(var item in emailresult){
message.To.Add(item);
}
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
If you can confirm what Type emailResults is it will be much easier to provide a clear solution.
In your comment above you state that emailresult is a select query result, so some sort of enumarable? Or as another poster suggested a long string separated by commas or semicolons?
Try doing one of these suggestions...
MailMessage message = new MailMessage("queensqsis#gmail.com", "queensqsis#gmail.com");// to & from
//For each item in the list, add it to the list of "To" recipients
emailresult.ForEach(r => message.To.Add(r)) ;
//OR IF THE ADDRESS IS A PROPERTY OF THE ITEM
emailresult.ForEach(r => message.To.Add(r.MyEmailAddressProperty)) ;
//OR IF emailresult is some sort of string list, "email1,email2,email3" or similar then
foreach (string oneEmail in emailresult.Split(","))
{
message.To.Add(oneEmail);
}
message.Subject = "Test";
message.Body = "test ";
SmtpClient Client = new SmtpClient();
Client.Send(message);
Using System.Web.Mail in an old project I used to use the following code to build an authenticated message
MailMessage msg = new MailMessage();
// ... fill in to, from etc
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", Application["smtpserver"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Application["smtpserverport"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", 2);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", Application["sendusername"].ToString());
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", Application["sendpassword"].ToString());
System.Net.Mail is recommended as the replacement, but it seems to have done away with these fields.
Here's my new email sending code.
MailMessage em = new MailMessage();
// ... fill in to, from etc
// Init SmtpClient and send
SmtpClient smtpClient =
new SmtpClient(
AppSetting["smtpserver"],
Convert.ToInt32(AppSetting["smtpserverport"])
);
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(
AppSetting["sendusername"],
AppSetting["sendpassword"]
);
smtpClient.Credentials = credentials;
smtpClient.Send(em);
I suspect that SmtpClient.Send is now doing all of this behind the scenes?
Yes, you don't have to add those fields manually.
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);
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".
I want to send a textBox value in email in C#. some one Help me?
see Scott Gu's article to send Sending Email with System.Net.Mail here.
Load the body of the mail with the TextBox.Text value
txtBox.Text;
will give you the value in C#
TextBox.Text Property
you can try something like this
MailMessage msg = new MailMessage();
msg.From = "test#mail.com";
msg.To.Add("rec#mail.com");
msg.Subject = "test";
msg.Body = yourTextbox.Text
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = _smtpHostName; //host name
smtpClient.Port = _smtpPort; //required port
smtpClient.Send(msg);
Have a look at
SmtpClient Class
and SmtpClient Constructor (String, Int32)