Hi all of you I try following code to send HTML email along with Image in HTML
but I can receive only text format mail not Image
public void HTML_mail(string mailTo,string mailSub,string mailMessage)
{
try
{
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
//client.Host = "smtp.gmail.com";
//client.Port = 587;
//WITH SMTP Server with Authenticaton
client.Host = mailServer;
client.Port = Convert.ToInt16(serverPort);
// setup Smtp authentication
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(userName, passWord);
client.UseDefaultCredentials = false;
client.Credentials = credentials;
MailMessage msg = new MailMessage();
msg.From = new MailAddress(userName);
msg.To.Add(new MailAddress(mailTo));
msg.Subject = mailSub;
msg.IsBodyHtml = true;
msg.Body = string.Format(mailMessage);
//HTML CODE "<html><head></head><body><p><h3>Dadu</h3></p><img src='http://localhost:2727/photo/mukeshwedsjashmin/1/Suresh2.jpg' height='500px' width='500px' alt='' /></body>"
try
{
client.Send(msg);
//lblMsg.Text = "Your message has been successfully sent.";
}
catch (Exception ex)
{
//lblMsg.ForeColor = Color.Red;
//lblMsg.Text = "Error occured while sending your message." + ex.Message;
}
}
catch(Exception ex)
{
}
}
I can Only see the "Dadu" in the mail
I choose display Image on my gmail A/C
You're email is referencing a localhost image, try it wih an online one as the image may not be available to the email.
Your email is referencing a local image:
http://localhost:2727/photo/mukeshwedsjashmin/1/Suresh2.jpg
The only e-mail receiver that will be able to see that image is yourself. No one else will have access to your local web server, thus won't be able to see the image.
You need to reference an image that is available to the public.
As a side note
In my experience, sending emails like this from a local mail server, especially if the email contain HTML and images, will almost certainly be caught as spam. I prefer to send my emails through an email delivery service. I only have experience with Postmark, which has a good .Net library, but I bet there are other great services as well.
Related
when i try to send a mail from my asp.net web forms could not send. while trying to debugging, time out issue occur. actually it was working fine in last year. but currently it is not working.i dont know what happen this code.
this is my aspx.cs code
public static void SendByMail(string responseBody, string Subject)
{
try
{
MailMessage message = new MailMessage();
message.From = new MailAddress("abc#gmail.com", "abcd.com");
message.To.Add(new MailAddress("myemail#gmail.com"));
message.Subject = Subject;
message.Body = responseBody;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = false;
NetworkCredential myCreds = new NetworkCredential("abc#gmail.com", "password", "");
client.Credentials = myCreds;
client.EnableSsl = true;
client.Send(message);
}
catch //(Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
Since March 2022, Google has strengthened the security of access to your account via a third-party application.
For this, you should not use your own password to log into your account, but rather generate a password via your account.
To do that, make sure that your two-step verification is on. Now, go to the "Manage your Google Account", then click the "Security" tab.
Now, click the App passwords option. Finally, you can select a Custom name for your app.
Once the password is generated, you will have to put it in place of your real password.
I'm trying to send email from asp.net mvc controller. Gmail account used here for smpt is configured to use with less security, so that's not the problem here.
but I don't get any error message neither any exception, but it not
deliver at my expected email address.
I'm using code
var text = "email body to deliver";
SendEmail("mydeliverEmailAddress#gmail.com", text);
public static bool SendEmail(string SentTo, string Text)
{
SmtpClient client = new SmtpClient();
client.Credentials = new NetworkCredential("myemail#gmail.com", "myGmailPass");
client.Port = 465;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
MailAddress
maFrom = new MailAddress("sender_email#domain.tld", "Sender's Name", Encoding.UTF8),
maTo = new MailAddress(SentTo, "Recipient's Name", Encoding.UTF8);
MailMessage mmsg = new MailMessage(maFrom.Address, maTo.Address);
mmsg.Body = "<html><body><h1>Some HTML Text for Test as BODY</h1></body></html>";
mmsg.IsBodyHtml = true;
mmsg.Subject = "Some Other Text as Subject";
mmsg.SubjectEncoding = Encoding.UTF8;
client.Send(mmsg);
}
catch (Exception ex)
{
}
return true;
}
Wait a minute. You are using your gmail account: myemail#gmail.com and trying to send an email on behalf of sender_email#domain.tld?
For more than obvious reasons that's never gonna work. So make sure that you are using the same email address as the one you are authenticating against:
maFrom = new MailAddress("myemail#gmail.com", "Sender's Name", Encoding.UTF8),
You can only send emails from the account you are authenticated against. Of course the recipient email can be any address that gmail can deliver to.
You've got another issue with your code. You are using a wring port here:
client.Port = 465;
The correct port that gmail SMTP works with is the following:
client.Port = 587;
Also you might want to ensure that you have enabled less secure apps in your gmail account or you will not be able to use SmtpClient in .NET to send emails using this SMTP: https://www.google.com/settings/security/lesssecureapps?pli=1
but I don't get any error message neither any exception, but it not
deliver at my expected email address.
What error message do you expect to get when you did the worst ever possible thing? You wrapped your code in a try/catch block and in your catch block you did absolutely nothing. You just consumed the exception:
catch (Exception ex)
{
}
So make sure that you do something useful with an exception if you are going to be catching it. For example something useful could be to log this exception and send an error message to the user saying that something bad happened and you couldn't send an email and that you are investigating the issue right now.
var smtpClient = new SmtpClient("YourSMTPServer", "SMTPServerPort"))
{
Credentials = new NetworkCredential("YourEmail",
"Password"),
EnableSsl = false
};
string fromEmail = "YourEmail";
var mailMessage = new MailMessage();
mailMessage.From = new MailAddress(fromEmail);
mailMessage.To.Add("Recipient's EMail");
mailMessage.Subject = "Test Mail";
mailMessage.Body = "This is test Mail";
mailMessage.IsBodyHtml = true;
smtpClient.Send(mailMessage);
SmtpClient smtpClient = new SmtpClient();
NetworkCredential basicCredential =
new NetworkCredential("sender#gmail.com", "password");
MailMessage message = new MailMessage();
MailAddress fromAddress = new MailAddress("sender#gmail.com");
smtpClient.EnableSsl = true;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredential;
message.From = fromAddress;
message.Subject = "your subject";
//Set IsBodyHtml to true means you can send HTML email.
message.IsBodyHtml = true;
message.Body = "<h1>Hello, this is a demo ... ..</h1>";
message.To.Add("receiver#gmail.com");
try
{
smtpClient.Send(message);
}
catch (Exception ex)
{
//Error, could not send the message
ex.ToString();
}
// The thing is that this code works fine for gmails without phone number protection. Exception occurs when using this code with gmails that are protected(verified) via the client phone number.
One of the solution is to use a remote server to access clients mails.
Now my question is there another method to solve this issue ? other than third parties.
If I understand you correctly, you're saying the Google account is using two-factor authentication.
If that's the case, you need to create an Application Password for this. Go to https://security.google.com/settings/security/apppasswords once logged in as the account you want to two-factor auth with.
In the list, under Select App choose "Other" and give it some name. Click Generate, and write this password DOWN cause you will only ever see it ONCE. You will use this in your authentication. It will be 16-characters long and the spaces don't matter, you can include them or omit them. I included them here just because.
NetworkCredential basicCredential =
new NetworkCredential("sender#gmail.com", "cadf afal rqcf cafo");
MailMessage message = new MailMessage();
I caught into big problem since am new to SMTP Email Server.
I have installed smtp server in my web server and configured the required details. My emails are now sending but in spam
I have implemented SendMail code using c# in my webapplication and I have one clarification on that.
string mailFrom = "Newsletter#my-domain.com";
string message = string.Empty;
System.Net.Mail.MailMessage email = new MailMessage(mailFrom, EmailAddress);
email.Subject = "Mail from my-domain.com";
email.Body = message;
email.IsBodyHtml = true;
email.Priority = MailPriority.High;
System.Net.Mail.SmtpClient mailClient = new SmtpClient();
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("username", "password");
mailClient.Host = "my-mail-server-domain.com";
mailClient.Port = 25;
mailClient.EnableSsl = false;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
try
{
mailClient.Send(email);
}
catch (Exception ex)
{
log4net.ILog logger = log4net.LogManager.GetLogger("File");
logger.Error(ex.ToString());
}
What value should I give for mailFrom. Is valid email is required for From address? Does it cause sending mail as spam? I don't have emailid in the name of newsletter#my-domain.com. What shall I do for that?
Please anyone clarify on this.
It is the responsibility of the mail server the validation of the address, all could be good or bad for c#. I think your focus is wrong.
The from should be a valid email address. That is the email from where an email is sent. The network credentials that you specify must match with the from address. Once you provide a valid email address, the emails will not go to spam folder hopefully.
This code works locally, but when I upload it to my server on Godaddy, it does not send the e-mail. Any idea why it doesn't work on their server? What do I need to change?
try {
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("Myemail#gmail.com");
mail.To.Add("Myemail#gmail.com");
mail.Subject = "New sign up";
mail.Body = "New member";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("Myemail#gmail.com", "**Mypass**");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
} catch(Exception ex) {
throw ex;
}
They may be blocking outgoing SMTP connections in order to prevent spammers from using their service to send spam. You should check what error messages you're getting and check your server host's policy.
There are a couple of things you need to do when sending from inside a site hosted from Godaddy. Use their relay server to send the message (this won't work from your dev machine, you'll have to test it live after you upload it). Here is the relay server info. Also make sure the "from" address is an email within the same domain. I usually use the same as the toAddress. See here for info on why this is necessary.
This is the code I'm using to send from a site inside Godaddy:
btnSend.Disabled = true;
const string serverHost = "relay-hosting.secureserver.net";
var msg = new MailMessage(toAddress, toAddress);
msg.ReplyTo = new MailAddress(emailFrom);
msg.Subject = subject;
msg.Body = emailBody;
msg.IsBodyHtml = false;
try
{
var smtp = new SmtpClient();
smtp.Host = serverHost;
smtp.Credentials = new System.Net.NetworkCredential("account", "password");
smtp.Send(msg);
}
catch (Exception e)
{
//Log the errors so that we can see them somewhere
}
You need to send your email via the godaddy smtp servers. I experienced the same issue with them before I think. I believe they give instructions of how to login via their FAQ.
If you have ssh access to the server, try to telnet smtp.google.com via 25 and 465 ports also. If you get a timeout, then you're likely firewalled from connecting to these ports outside a certain IP range.
Port 587 is for TLS. As you're using SSL, try port 465.