I've got a c# process that send emails to customers with a hyperlink in the mail. The mail is send from a SQL Server stored proc. My c# program just invokes the sp.
The hyperlink works fine in Outlook, but on online gmail it only shows as text. It is not clickable.
My mail text looks something like:
Hi.
This is the hyperlink:<br>
<a href=\"serveraddress\Documents\\123_128635312685687531322.gif\">
Click Here</a><br><br>
What should I do to fix it?
EDIT:
My code:
string email = "xx#gmail.com;
string password = "MyPassword";
var credentials = new NetworkCredential(email, password);
var msg = new MailMessage();
var smtpClient = new SmtpClient("smtp.gmail.com", 587);
msg.From = new MailAddress(email, senderName);
msg.To.Add(new MailAddress(toAddress));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = credentials;
smtpClient.Send(msg);
EDIT 2:
Compile message being send:
string message = #"Hi. <br>
This is the intro line in the mail message.<br>";
using (DataTable dtLinks = data.ExecuteDataSet(#"SELECT *
FROM LessonFiles
WHERE Course = " + dr["Course"].ToString().DBValue() + #" AND
Lesson = " + dr["NextLesson"].ToString().DBValue()).Tables[0])
{
int i = 0;
foreach (DataRow drLink in dtLinks.Rows)
{
i += 1;
message += "<a href=\"" + drLink["Link"].ToString() + "\">" + drLink["Lesson"].ToString();
message += i == 1 ? "" : " file " + i;
message += "</a>" + "<br>";
}
}
message += "<br>Regards<br><br>";
try to add target="_blank", like this...
message += "<a href=\"" + drLink["Link"].ToString() + "\"target=\"_blank\">" + drLink["Lesson"].ToString();
Seems like something was funny with the hyperlink itself.
Using http://serveraddress/Documents/logoColourBG635315550177822533.jpg seems to work.
The original contained backslashes in the path. The fact that it showed the hyperlink in Outlook led me to believe the address was correct.
Thanks for all your help.
creating mail message object...
var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(UserName, Password);
smtp.Timeout = 20000;
MailMessage Msg = new MailMessage();
Msg.IsBodyHtml = true;
MailAddress fromMail = new MailAddress(SenderID);
Msg.From = fromMail;
Msg.To.Add(new MailAddress(TosendID));
Msg.Subject = subject;
Msg.Body = body;
In body add ur code.....
Hope This Helps.........
Related
I want to automate my work by sending the screenshot taken during automation in tabular email format . I had successfully attached as document, but I want it in tabular format. This is code what I had already tried
public static void email_send() {
string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:test1.jpeg\"></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource("test1.jpeg", MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
avHtml.LinkedResources.Add(inline);
Attachment att = new Attachment(#"D:/test123.png");
att.ContentDisposition.Inline = true;
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
System.Net.Mail.SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("*****");
mail.To.Add("*******");
mail.Subject = "Test Mail - 1";
mail.Body = String.Format(
"<h3>Client: " + " Has Sent You A Screenshot</h3>" +
#"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("******", "******");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
You have missed the attachment.
Your linked resource also needs to be attached to the email. You need to add the following code:
mail.Attachments.Add(att);
In MyCalendar_SelectedDatesChanged() event i am showing all multiple selected dates in list box. I need to pick all these dates and add in email body line by line.
sending mail code :
login = new NetworkCredential("wapsatest#gmail.com", "wapsatest123456");
client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt32(587);
client.EnableSsl = true;
client.Credentials = login;
msg = new MailMessage { From = new MailAddress("wapsatest" + "smtp.gmail.com".Replace("smtp.", "#"), "nWorks Employee", Encoding.UTF8) };
msg.To.Add(new MailAddress("saurabh.pawar#nworks.co"));
msg.Subject = "Requested for leave by "+comboboxEmployee.Text;
msg.Body = "///////////////List of dates coming from list box name selecteddates";
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userstate = "sending.......";
client.SendAsync(msg, userstate);
how can i add required body using html format in c# wpf...?
Try below code that may help you.
login = new NetworkCredential("wapsatest#gmail.com", "wapsatest123456");
client = new SmtpClient("smtp.gmail.com");
client.Port = Convert.ToInt32(587);
client.EnableSsl = true;
client.Credentials = login;
msg = new MailMessage { From = new MailAddress("wapsatest" + "smtp.gmail.com".Replace("smtp.", "#"), "nWorks Employee", Encoding.UTF8) };
msg.To.Add(new MailAddress("saurabh.pawar#nworks.co"));
msg.Subject = "Requested for leave by "+comboboxEmployee.Text;
//-------------------Code for your Email body---------------------
string strBody = string.Empty;
int i = 1;
strBody += "///////////////List of dates coming from list box name selecteddates";
strBody += Environment.NewLine;
foreach (var item in lstDate) // here "lstDate" is name of your list where you store all date.
{
//strBody += item.ToShortDateString() + Environment.NewLine;
strBody += "Some text before date".
strBody += i + ". " + item.ToShortDateString() + " (" + item.DayOfWeek + ")";
strBody += "Some text after date".
strBody += "<br/>";
i++;
}
msg.Body = strBody;
//----------------------------- Over -----------------------------
msg.BodyEncoding = Encoding.UTF8;
msg.IsBodyHtml = true;
msg.Priority = MailPriority.Normal;
msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
string userstate = "sending.......";
client.SendAsync(msg, userstate);
I want to send mail from exchange server without password. I use the following code snippet :
MailMessage msg = new MailMessage();
msg.From = new MailAddress("kavitham#test.com");
msg.To.Add(new MailAddress("kavitham#sample.com"));
msg.Subject = "Reg : Mail test";
msg.IsBodyHtml = true;
msg.Body = "<html><body>" + strMsg + "</body></html>";
SmtpClient client = new SmtpClient("IP of Server", 25);
client.Host = "IP of Server";
client.Send(msg);
But the mail is not sent. Is there any other settings for Exchange server need to be done ?
add this
smtpClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
MailMessage msg = new MailMessage("noreply#test.com", "manager#test.com", "Notification For Lead / Admin ",
"Emp Name :" + EmpName);
msg.Body = "Emp Name : " + EmpName + "<br /><br >" + Environment.NewLine + timsheetMail;
msg.IsBodyHtml = true;
msg.CC.Add(toMail); msg.CC.Add(admin);
SmtpClient SMTPServer = new SmtpClient("mail.myserver.com", 25);
SMTPServer.Timeout = 300000;
SMTPServer.EnableSsl = false;
SMTPServer.Credentials = new System.Net.NetworkCredential("noreply#test.com", "passw0rd");
SMTPServer.Send(mailObj);
I have created registration page which will send automatically email users to confirm their account. Is there any way to send conformations emails to different domains like hotmail,yahoo,live,etc.. all different domains(including .com,.uk,.org, etc..,). If so can i get code/article to how to use the code/modify my code in c#.. Here is my code for sending email.
using (MailMessage mm = new MailMessage("sender#mydomain.com", txtEmail.Text))
{
mm.Subject = "Account Activation";
string body = "Hello " + txtUsername.Text.Trim() + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
NetworkCredential NetworkCred = new NetworkCredential("sender#gmail.com", "password");
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = NetworkCred;
smtp.Send(mm);
}
You don't need to do anything different to send to different email domains.
I have an email form on my website which is working great. The only thing I want to do is add usings for better coding, what is the best way to do this? The code I use is the following:
try
{
//create message
MailMessage msg = new MailMessage();
msg.To.Add(txtTo.Text);
msg.From = new MailAddress(txtFrom.Text);
msg.Subject = string.Format("Van: " + txtName.Text + " | Email: " + txtFrom.Text + " | Onderwerp: " + txtSubject.Text);
msg.Body = txtBericht.Text;
SmtpClient smtp = new SmtpClient("");
smtp.Host = "smtp.live.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("emailaddress", "pass");
smtp.Send(msg);
ClearTextboxes();
lblError.ForeColor = Color.Green;
lblError.Text = "Het verzenden van uw e-mail is gelukt!";
}
catch
{
lblError.ForeColor = Color.Red;
lblError.Text = "Er ging iets mis met het versturen van de email.";
}
How can I use usings on this code?
And is it safe to use fill in my emailaddress and password when I'm uploading my website, or should I use another method?
Thanks in advance!
EDIT:
I changed my code to the following:
try
{
//create message
using (MailMessage msg = new MailMessage())
{
//create message
MailMessage msg = new MailMessage();
msg.To.Add(txtTo.Text);
msg.From = new MailAddress(txtFrom.Text);
msg.Subject = string.Format("Van: " + txtName.Text + " | Email: " + txtFrom.Text + " | Onderwerp: " + txtSubject.Text);
msg.Body = txtBericht.Text;
using (SmtpClient smtp = new SmtpClient())
{
smtp.Host = "smtp.live.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("emailadress", "pass");
smtp.Send(msg);
}
ClearTextboxes();
lblError.ForeColor = Color.Green;
lblError.Text = "Het verzenden van uw e-mail is gelukt!";
}
}
catch
{
lblError.ForeColor = Color.Red;
lblError.Text = "Er ging iets mis met het versturen van de email.";
}
Never put authentication information in your code. Put it in your web.config instead and reference it where you need it. That also saves you the trouble of hunting down everywhere you used it if you change your password.
I would suggest using the web.config for setting up the host, username and password. Example.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="name#domain.com">
<network host="smtp.mail.com"
userName="name#domain.com"
password="blog.dotnetclr.com" port="25"/>
</smtp>
</mailSettings>
</system.net>
Code Example:
Public Sub SendEmail([to] As String, subject As String, body As String)
Dim mailMessage = New System.Net.Mail.MailMessage()
mailMessage.[To].Add([to])
mailMessage.Subject = subject
mailMessage.Body = body
Dim smtpClient = New SmtpClient()
smtpClient.EnableSsl = True
smtpClient.Send(mailMessage)
End Sub
http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.100).aspx
using (MailMessage msg = new MailMessage())
{
//Your Code
}
You could use using for both MailMessage and SMTP Client objects
//create message
using (SmtpClient smtp = new SmtpClient(""))
using( MailMessage msg = new MailMessage())
{
// Your code
}
using(SmtpClient smtp = new SmtpClient()){
//smtp options here
using(MailMessage msg = new MailMessage()){
//mail options here
smtp.Send(msg);
}
}