how to send mail in asp.net - 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"));

Related

Send Email to GoDaddy using asp.net

I am using below code to send email to webmail GoDaddy and it is working fine from my local machine and when deployed on client server it is not working.
Below is my code :
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
MailAddress fromAddress = new MailAddress("xyz#com");
message.From = fromAddress;
message.To.Add("abc#gmail.com");
message.Subject = "test Message";
message.IsBodyHtml = true;
message.Body = "we are here";
smtpClient.Host = "mail.trustwellhospitals.com";
smtpClient.Port = 25;
smtpClient.EnableSsl = false;
smtpClient.UseDefaultCredentials = false;
System.Net.NetworkCredential("username", "password");
smtpClient.Send(message);

How to get Dynamic Data in email newsletter from the website in asp.net c#?

I have a newsletter design in which i have to bind data dynamically and send mail to users in asp.net c# through noreply#xyz.com
I tried but getting error of authentication if anyone knows please sort out the problem.
MailMessage Msg = new MailMessage();
MailAddress fromMail = new MailAddress("abc#gmail.com");
// Sender e-mail address.
Msg.From = fromMail;
// Recipient e-mail address.
Msg.To.Add(new MailAddress("xyz#gmail.com"));
// Subject of e-mail
Msg.Subject = "Send Gridivew in EMail";
Msg.Body += "Please check below data <br/><br/>";
Msg.Body += GetGridviewData(gvUserInfo);
Msg.IsBodyHtml = true;
string sSmtpServer = "";
sSmtpServer = "smtp.gmail.com";
SmtpClient a = new SmtpClient();
a.Host = sSmtpServer;
a.EnableSsl = true;
a.Port = 587;
a.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
a.Credentials = new NetworkCredential("abc#gmail.com", "abc#123");
a.Send(Msg);
Grid View Code
public string GetGridviewData(GridView gv)
{
StringBuilder strBuilder = new StringBuilder();
StringWriter strWriter = new StringWriter(strBuilder);
HtmlTextWriter htw = new HtmlTextWriter(strWriter);
gv.RenderControl(htw);
return strBuilder.ToString();
}

Sending Email through Smtp

i'm unable to send mail, exception "Failure sending mail"
what should i do to correct the code?
MailMessage mailMessage = new MailMessage("yourmail#mail.com", ToEmail);
StringBuilder sbEmailBody = new StringBuilder();
sbEmailBody.Append("<b>Registration Approval for</b></br>");
sbEmailBody.Append(InstituteName + "<br/><br/>");
sbEmailBody.Append("Thanks for your registration ");
sbEmailBody.Append("<br/>");
mailMessage.IsBodyHtml = true;
mailMessage.Body = sbEmailBody.ToString();
mailMessage.Subject = "Confirmation Mail!";
SmtpClient smtpClient = new SmtpClient("smtp.mail.com", 587);
smtpClient.Credentials = new System.Net.NetworkCredential()
{
UserName = "yourmailn#mail.com",
Password = " "
};
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);

Asp.net 4.0 Sending Emails

I have aspx page where i have the email field like this
<input class="span12" type="text" placeholder="EMAIL" id="Email" name="Email" runat="server" />
In my Csharp file i have code and using Request["Email"] to get the address when visitor enter the email address which can be any so i want to email them as well my code is like Below, But it does not work, I am using .net 4.0, where i can change then that dynamic email whatever it would be i could get it and send email.
private void SendEmail(int RefNum)
{
var customerEmail = Request["Email"]; //getting value from aspx page.
MailMessage ObjEmail = new MailMessage();
ObjEmail.SendFrom = "carlaza#hotmail.ca";
ObjEmail.SendTo = "zjaffary#hotmail.com";
ObjEmail.SendCC = "jaffary_zafar#hotmail.com";
ObjEmail.SendBCC = customerEmail ;
ObjEmail.Subject = "test Subject ";
//Development
//SmtpMail.SmtpServer = "tormail.corp.kkt.ca";
//Production At Bell
SmtpMail.SmtpServer = "tormail.corp.kkt.ca";
ObjEmail.BodyFormat = MailFormat.Html;
string strBody1 = "Test message " ;
ObjEmail.Priority = MailPriority.High;
try {
SmtpMail.Send(ObjEmail);
lblResponse.Text = "Thank you for sending the form !";
Response.AddHeader("Refresh", "2;URL=index.aspx");
}
catch (Exception exc){
Response.Write("Send failure: " + exc.ToString());
}
}
You should use authetication information from web mail server. (user name and password) If not, that is not actual e-mail.
You can see the code and can work
SmtpClient SmtpServer = new SmtpClient("smtp.live.com");
var mail = new MailMessage();
mail.From = new MailAddress("youremail#hotmail.com");
mail.To.Add("to#gmail.com");
mail.Subject = "Test Mail - 1";
mail.IsBodyHtml = true;
string htmlBody;
htmlBody = "Write some HTML code here";
mail.Body = htmlBody;
SmtpServer.Port = 587;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Credentials = new System.Net.NetworkCredential("youremail#hotmail.com", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
Please see the topic. I think it is helpfull for you.
How to add smtp hotmail account to send mail
How to add smtp hotmail account to send mail
try this
protected void sendEmail(string subject, string ToEmail, string msg)
{
String body = msg;
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("your_email_id");
smtpClient.Host = "smtp.gmail.com";//host
smtpClient.Port = 587;//port no. default 25
smtpClient.UseDefaultCredentials = false;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential("your_email_id", "password");
message.From = fromAddress;
message.To.Add(ToEmail);//if more than comma seprated
message.Subject = subject;
message.Priority = MailPriority.High;
message.Body = body;
message.IsBodyHtml = true;
smtpClient.Send(message);
}

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

Resources