ASP.NET how to write code for mail sender? - asp.net

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

Related

Email with SendGrid is not working in Azure Apps

I need to send email from my code using the below code snippet. It is working fine in my code, but when I deploy the code in microsoft azure I am getting an error as follows.Kindly help me to rectify the issue.
MailMessage mail = new MailMessage();
mail.From = new MailAddress(FromAddress);
mail.To.Add(email);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient SmtpServer = new SmtpClient("smtp.sendgrid.net");
SmtpServer.Port = 587;
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential myCreds = new NetworkCredential(EmailUsername, EmailPassword);
SmtpServer.EnableSsl = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = myCreds;
SmtpServer.Send(mail);
Error as Follows
Finally I found a Solution, I have written some code for getting OS version and Browser version. That was creating issue.

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

how to attach a file in email using shared hosting server?

I want to attach a file dynamically and send it through mail. So can someone please let me know how can i send it with attachment.
You can use this code.
using System.Net.Mail;
private void SendEmail()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("YourEmailAddress#domain.com");
message.To.Add(new MailAddress("Recipient#domain.com"));
message.Subject = "Subject";
message.Body = "Email Message Body";
// Add attachment
string attachmentPath = Server.MapPath("~/AttachmentPath.jpg");
message.Attachments.Add(new Attachment(attachmentPath));
// Connect to GoDaddy SMTP
SmtpClient smtp = new SmtpClient("relay-hosting.secureserver.net");
smtp.Credentials = new System.Net.NetworkCredential("Username","Password");
smtp.Port = 25;
smtp.EnableSsl = false;
// Send the message
smtp.Send(message);
}
you can try with this code
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("...");/:Adjust your adress
mail.From = new MailAddress("your_email_address#gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("your attachment file");
mail.Attachments.Add(attachment);
SmtpServer.Port = ..; //Replace with your port number
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

sending to an email with contact form 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);

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