sending to an email with contact form asp.net - asp.net

How would I go about sending an email to a specified email address with a contact form in asp.net? The website is hosted though a hosting company. thanks

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("your.mail.server");
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address#mfc.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail";
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.Send(mail);

Related

ASP.NET how to write code for mail sender?

I have search many sites. But I can't understatnd what they said. Refer in internet they told to create CDO files.
Please help me to send mail in my Project. I have 3 textboxes:
Tomail
Heading
Content of the message
Send Button
Use Below Code to sent mail.
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("me#mydomain.com");
mail.To.Add("u#urdomain.com,rkrishtring#gmail.com");
mail.Subject = "Report";
mail.Body = "This is a Stack overflow using report";
Attachment attachment = new Attachment(filename);
mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

ASP.NET sending with Gmail smtp overrides From Address

I was trying to send from a aspx page a mail message using the smtp gmail (we are google apps customers and I tried with a paid account and with a free gmail...)
I was able to send the messagge setting credentials, port, ssl, etc. but what is wrong is the From address of the message. Indeed, the mail is coming always from the address of the auth user and not from what I set in the msg.From property...
Eg. the mail arrives with from "myaccount#gmail.com" and not from sender#somedomain.com
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sender#somedomain.com", "sender name");
msg.To.Add(new MailAddress("recipient#somedomain.com","recipient name"));
msg.Subject = "subject";
msg.Body = "some body";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("myaccount#gmail.com", "*******");
smtp.EnableSsl = true;
smtp.Send(msg);
Is there a setting to avoid this ?
thanks in advance
Sandro
Check the message headers.
Depending on your email client, here's the instructions how to reach them: https://support.google.com/mail/answer/22454?hl=en
You'll see, that FROM field is only for indication of the sender's name.
It can be ignored by email client, especially, if it contains address different than real sender's address.

Gmail message is rejected

I am trying to send emails from ASP.net project using "System.Net.Mail.MailMessage". I need to add about 30 contacts to "To" or "BCC" list.
It works for few recipients but when the number increases;
Gmail give error:
"Message rejected. See http://support.google.com/mail/bin/answer.py?answer=69585 for more information."
And block my accont for sending any more emails for 1 day.
How can i allow gmail to send email to many recipients?
If not what is the alternative way?
You should be able to comfortably send to 30 addresses.
I tried 40 email addresses for CC list (worked for To list as well) with the following test code,
string mailFrom = "test123#gmail.com";
string mailTo = "testccc#domain.com"; //One of your test emails
MailAddress to = new MailAddress(mailTo);
MailAddress from = new MailAddress(mailFrom);
MailMessage mail = new MailMessage(from, to);
mail.Subject = "Test subject";
mail.Body = "Hello world!!!!";
//Keep adding your emails as follows, do it in a for loop
mail.CC.Add("dhanuka1#domain.com");
mail.CC.Add("dhanuka2#domain.com");
...
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new NetworkCredential("mydev#gmail.com", "password");//Your credentials
smtp.EnableSsl = true;
Console.WriteLine("Sending email...");
smtp.Send(mail);
Console.WriteLine("Email sent...");
Console.ReadLine();

Send email with System.Web.Mail

I want send email in asp.
I use this code
using System.Web.Mail;
MailMessage msg = new MailMessage();
msg.To = "aspnet#yahoo.com";
msg.From = "info#mysite.com";
msg.Subject = "Send mail sample";
msg.BodyFormat = MailFormat.Html;
string msgBody="Hello My Friend. This is a test.";
msg.Body = msgBody ;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(msg);
But i get error :
Bad sequence of commands. The server response was: This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server.
How to send email with asp?
I use This code .
MailMessage msg = new MailMessage();
msg.Body = "Body";
string smtpServer = "mail.DomainName";
string userName = "info#mysite.com";
string password = "MyPassword";
int cdoBasic = 1;
int cdoSendUsingPort = 2;
if (userName.Length > 0)
{
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
}
msg.To = user.Email;
msg.From = "info#Mysite.com";
msg.Subject = "Subject";
msg.BodyEncoding = System.Text.Encoding.UTF8;
SmtpMail.SmtpServer = smtpServer;
SmtpMail.Send(msg);
You might need to provide credentials.
example:
smtpMail.Credentials = new NetworkCredential("username", "password")
If you are trying to send email without authenticating, I am afraid that's impossible. If any users in your web site can send emails without password, that's horrible. It will allow user to send email from other person account. So considering security, sending email will required to provide email address and password
var fromAddress = ""; // Email Address here. This will be the sender.
string fromPassword = ""; // Password for above mentioned email address.
var toAddress = "";// Receiver email address here
string subject = "Hi";
string body = "Body Text here";
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "smtp.gmail.com"; // this is for gmail.
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
smtp.Timeout = 20000;
}
smtp.Send(fromAddress, toAddress, subject, body);
[Edited]
Sorry My mistake i didn't noticed that. They both used for same purpose. If you are using higher version (2.0 or later) of .Net framework, then use System.Net.Mail. If you use System.Web.Mail it only shows a warning saying this is deprecated. But that will work.
Here is the answer for System.web.mail
MailMessage mail = new MailMessage();
mail.To.Add("to#domain.com");
mail.From = new MailAddress("from#domain.com");
mail.Subject = "Email using Gmail";
mail.Body = "";
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential(mail.From,"YourPassword");
smtp.Send(mail);

how to send mail in asp.net

hii
i m trying to send mail through coding of asp
is there any external APIs to send mail like JAVA
give some hints
if possible sample code!!
I m using vs 2005 as well as vs 2008
You could use the SmtpClient class. Example using GMail's SMTP:
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);
UPDATE:
Example with 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);
Try this:
using System.Web.Mail;
private void SendMessage()
{
MailMessage mail = new MailMessage();
mail.To = txtTo.Text;
mail.From = txtFrom.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(mail);
}
if want to send attachment
Add the following code
mail.Attachments.Add(new MailAttachment(#"C:\myFile.txt"));

Resources