I have an ASP.NET 4.0 aspx page from which I wish to send an email to the recipient specified in a text box named "supervisoremailTextBox". Is there any way that I can specify a variable as the recipient email address. The code I have used which doesn't work is shown below:
MailAddress fromAddress = new MailAddress("address#domain.co.uk", "Sender Name");
MailAddress toAddress = new MailAddress("supervisoremailTextBox.Value");
message.From = fromAddress;
message.To.Add(toAddress);
Sorry if this a really dumb question and thanks in advance for your help.
When you use MailAddress, you need to use a valid email address.
The string "supervisoremailTextBox.Value" is not a valid email address.
If you mean to use the value of a textbox with the ID supervisoremailTextBox, use:
MailAddress toAddress = new MailAddress(supervisoremailTextBox.Value);
Note that I have dropped the " to ensure you are not passing in a string.
Try this instead:
MailAddress toAddress = new MailAddress(supervisoremailTextBox.Value);
Related
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);
I am using Windows Form Application which is sending HTML emails using MailDefinition Class.
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = strfilename;
mailDefinition.From = "email#company.com";
Is it possible that somehow I can add a different email id where user will reply. I don't want the users to reply on this email#company.com email id.
I am looking for some thing like this:
mail.Headers.Add( "Reply-To", "alternate_email#mycompany.com")
I use the System.Net.Mail.SmtpClient class which has a send method that takes a System.Net.Mail.MailMessage parameter.
In the MailMessage class it has: replyTo and replyToList properties.
Add HTML content like this:
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage();
m.Body = "HTML Content";
m.IsBodyHtml = true;
I want to send email with EWS using custom display name.
Here is my Code:
EmailMessage message = new EmailMessage();
message.From = new EmailAddress("someone","one#two.com");
My expectation is that the received email to come from: someone <one#two.com>
Instead of this I get the email from: one <one#two.com>
Anybody know what would be the problem here?
EDIT
Have you tried this?
new EmailAddress("someone <one#two.com>;");
PREVIOUS
System.Net.Mail Namespace has MailAddress class, not EmailAddress.
Are you using System.Net.Mail? And if not why not?
MailAddress Constructor (String, String) is this:
public MailAddress(
string address,
string displayName
)
so you should use this way:
message.From = new MailAddress("one#two.com","someone");
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.
WebMail.SmtpServer = SmtpServer;
WebMail.UserName = SmtpUsername;
WebMail.Password = SmtpPassword;
WebMail.Send(
ReplayEmail,
subject,
body,
email
);
subject is for example "Ask specialist"
body is html body
email is for example "domain#domain.com"
email is send to for example "domain2#domain.com"
now when i send mail i get mail which is ok but as person who send mail I see "domain2#domain.com". how to change this?
mail now looks like
domain2#domain.com Ask specialist
i want name (Adoo for example) and mail like
Adoo Ask specialist
If I understand correctly, when setting up mail you can do :
mail.To = new MailAddress("domain2#domain.com", "Adoo");
or :
mail.To= #"\Adoo \ <domain2#domain.com>";
I'm assuming ReplayAddress is a System.Net.Mail.MailAddress defined in code somewhere else you haven't shown. MailAddress has a constructor which takes a string displayName which should do what you want if I understand you correctly - documentation on that is here.
Typically in SMTP you can create a friendly name by using a format like this:
WebMail.Send(
"\"Adoo\" <domain2#domain.com>",
subject,
body,
email
);
or you can use
mail.To= #""Adoo" <domain2#domain.com>";
You are using the WebMail class which takes a string for the .From property of the class. I would use the MailAddress class and do:
MailAddress from = new MailAddress("domain2#domain.com", "Adoo Ask specialist");