Mandrill Template is sent correctly once every 28 times - c#

When I run the code below the Mandrill Template is sent correctly once every 28 times. Every other time it will send "Test_email". Does anyone know why this is? Can this be a trial version?
html:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
Table
<table style="text-align: center; border-spacing:5px; margin-left: auto;margin-right: auto;vertical-align: middle;width: 600px;background-color:lightgrey; padding-top:15px; padding-bottom:15px; padding-left:10px;padding-right:10px;border-radius:15px;">
<tr>
<td>Some Text here</td>
</tr>
</table>
</body>
</html>
C#
namespace Messages
{
class Program
{
static void Main(string[] args)
{
var fromAddress = new MailAddress("me#mail.com", "b");
var toAddress = new MailAddress("ano#gmail.com", "To Name");
const string fromPassword = "API Key";
const string subject = "test template email";
//const string body = "Test-email";
TemplateContent tp = new TemplateContent();
tp.name = "Test_email";
var smtp = new SmtpClient
{
Host = "smtp.mandrillapp.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = tp.name
})
{
smtp.Send(message);
}
}
}
}

If they're showing in the Outbound Activity, then your code was fine and Mandrill processed the mail. Is the issue that it shows as delivered in Mandrill but you don't see it? Or that it doesn't look like the template was applied? If you're sending to Gmail, you might see the content hidden because of the emails being included in a conversation. Look for three dots to expand the information. If they all have the same subject line and content, then Gmail will hide previously-received content as part of the conversations feature. If that's not the issue, then you should clarify exactly where you're seeing an issue in the process.

Related

How to design SMTP mail content in c#

Software that I'm developing right now sends e-mail to responsible when a problem occurs in a production line. However while doing this email,I prefer it with a design like a card not a normal string.
I have just a little knowledge HTML and tried it but result was terrible.
So is there any API for c# to make this?
Look at this sample. May be it can help you.
MailAddress addressFrom = new MailAddress("jack.du#e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32#outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);
message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = #"<html>
<body>
<p>Dear Ms. Susan,</p>
<p>Thank you for your letter of yesterday inviting me to come for
an interview on Friday afternoon, 5th July, at 2:30.I shall be happy to be there as
requested and will bring my diploma and other papers with me.</p>
<p>Sincerely,<br>-Jack</br></p>
</body>
</html>
";
message.BodyHtml = htmlString;
SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);
Console.WriteLine("Sent Successfully!");
Console.Read();
You should use the below property in order to get your content in the Html format.
message.IsBodyHtml = true;
Hope this is helpful

How to click button and open window to send email in c#

I want to create a button so that user can click and it will open a small new window where they can send email. This window will have "From", "To", "Subject", "Content" fields and all of that will have default text, user can edit them (except for "From" field). See image below:
What I tried:
I created an email form by:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<p>
From:<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</p>
To:<asp:Literal ID="Literal2" runat="server"></asp:Literal>
<p>
Subject:<asp:Literal ID="Literal3" runat="server"></asp:Literal>
</p>
Content:<asp:Literal ID="Literal4" runat="server"></asp:Literal>
</form>
</body>
</html>
Then I try to link this form with my current code:
Current code: I can send email by this code:
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtpclientaddresshere");
mail.From = new MailAddress("defaultFromEmail#domain.com");
mail.To.Add("email1#yahoo.com,email2#yahoo.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail";
SmtpServer.Credentials = new System.Net.NetworkCredential("mysmtpserver#something.com", "");
SmtpServer.Send(mail);
Now I don't know is this right to use the form above for email window purpose? And how could I format and link all the fields to my working code?
What I have done is like this:
public void SendEmail(string _from, string _fromDisplayName, string _to, string _toDisplayName, string _subject, string _body, string _password)
{
try
{
SmtpClient _smtp = new SmtpClient();
MailMessage _message = new MailMessage();
_message.From = new MailAddress(_from, _fromDisplayName); // Your email address and your full name
_message.To.Add(new MailAddress(_to, _toDisplayName)); // The recipient email address and the recipient full name // Cannot be edited
_message.Subject = _subject; // The subject of the email
_message.Body = _body; // The body of the email
_smtp.Port = 587; // Google mail port
_smtp.Host = "smtp.gmail.com"; // Google mail address
_smtp.EnableSsl = true;
_smtp.UseDefaultCredentials = false;
_smtp.Credentials = new NetworkCredential(_from, _password); // Login the gmail using your email address and password
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
_smtp.Send(_message);
ShowMessageBox("Your message has been successfully sent.", "Success", 2);
}
catch (Exception ex)
{
ShowMessageBox("Message : " + ex.Message + "\n\nEither your e-mail or password incorrect. (Are you using Gmail account?)", "Error", 1);
}
}
And I am using it like this:
SendEmail(textBox2.Text, textBox5.Text, textBox3.Text, "YOUR_FULL_NAME", textBox4.Text, textBox6.Text, "YOUR_EMAIL_PASSWORD");
Here is the image:
(Although I am using WinForms and not Windows Presentation Forms).
May this answer would help you.
Cheers!

send email as template in asp.net

i am able to send and receive emails through my application, but its jus plain text content. I want to send full formatted content decorated with css and div or tables.
How to do it?
I am using asp.net 3.5 VS2010.
public void Send(string To, string Subject, string Body)
{
System.Net.Mail.MailMessage m = new System.Net.Mail.MailMessage("mymail#gmail.com", To);
m.Subject = Subject;
m.Body = Body;
m.IsBodyHtml = true;
m.From = new MailAddress("mymail#gmail.com");
m.To.Add(new MailAddress(To));
SmtpClient smtp = new SmtpClient();
smtp.Host = "198.208.0.***";
NetworkCredential authinfo = new NetworkCredential("mymail#gmail.com", "password");
smtp.UseDefaultCredentials = false;
smtp.Credentials = authinfo;
smtp.Send(m);
}
calling this function like
Send("mynewmail#gmail.com", "testmail", "new test body");
this is my function.
The answer is here : http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/c65502fa-955e-4fa1-b409-238bbb677df7
I think the main thing you're missing is the MIME Content Type on the LinkedResource.
myMagic.css:
body {
background-color: rgb(240,240,240);
font-family: Verdana,Arial,Helvetica,Sans-serif;
font-size: 10pt;
}
h2 {
background-color: rgb(255,255,128);
color: rgb(255,0,0);
}
p {
background-color: rgb(0,0,255);
color: rgb(0,255,0);
font-style: italic;
}
Program.cs:
using System.Net.Mail;
using System.Net.Mime;
namespace MailMessageHTML
{
class Program
{
private static MailMessage ConstructMessage(string from, string to, string subject)
{
const string textPlainContent =#"You need a HTML-capable mail agent to read this message.";
const string textHtmlContent =#"<html><head><link rel='stylesheet' type='text/css' href='cid:myMagicStyle' />
</head>
<body>
<h2>Hello world!</h2>
<p>This is a test HTML e-mail message.</p>
</body>
</html>
";
MailMessage result = new MailMessage(from, to, subject, textPlainContent);
LinkedResource cssResource = new LinkedResource("myMagic.css", "text/css");
//NOTE: Message encoding adds the surrounding <> on this Id cssResource.ContentId =myMagicStyle";
cssResource.TransferEncoding = TransferEncoding.SevenBit;
AlternateView htmlBody = AlternateView.CreateAlternateViewFromString( textHtmlContent , new ContentType("text/html"));
htmlBody.TransferEncoding = TransferEncoding.SevenBit;
htmlBody.LinkedResources.Add(cssResource);
result.AlternateViews.Add(htmlBody);
return result;
}
static void Main(string[] args)
{
MailMessage foo = ConstructMessage(
"sender#foo.com"
,"recipient#bar.com"
, "Test HTML message with style."
);
SmtpClient sender = new SmtpClient();
sender.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
sender.PickupDirectoryLocation = #"...\MailMessageHTML\bin\Debug\";
sender.Send(foo);
}
}
}
NOTE 1: I've specified TransferEncoding.SevenBit on the message parts above. You wouldn't normally do this in a production environment - I've only done it here so you can read the resultant .eml file with Notepad to see what was generated.
NOTE 2: A lot of mail agents won't honour stylesheets linked in message parts. A more-reliable way might be to use inline styles (i.e.: inline ... blocks within the HTML message body).
Good luck,
This answer got from : http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/c65502fa-955e-4fa1-b409-238bbb677df7
If you don't need dynamic content (if/else or foreach) or model binding. I think put your template into a text file and use string.Format() is sufficient.
Otherwise, you'll need a template engine. This question may be a good reference for you.
Have a look at MvcMailer on github
It uses a viewengine to render emails so you have all the benefits of viewmodels and templating.
Have a look at http://www.campaignmonitor.com/css/ which shows how to apply CSS to emails.
Also, use inline CSS.. And are you setting emailMessage.IsBodyHtml = true; ?

How to send email with multiple addresses in C#

I'm trying to send emails using gmail's username and password in a Windows application. However, the following code is sending the mail to only the first email address when I collect multiple email address in my StringBuilder instance.
var fromAddress = new MailAddress(username, DefaultSender);
var toAddress = new MailAddress(builder.ToString());//builder reference having multiple email address
string subject = txtSubject.Text;
string body = txtBody.Text; ;
var smtp = new SmtpClient
{
Host = HostName,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(username, password),
//Timeout = 1000000
};
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
IsBodyHtml = chkHtmlBody.Checked
};
if (System.IO.File.Exists(txtAttechments.Text))
{
System.Net.Mail.Attachment attechment = new Attachment(txtAttechments.Text);
message.Attachments.Add(attechment);
}
if(this.Enabled)
this.Enabled = false;
smtp.Send(message);
What am I doing wrong, and how can I sort out my problem?
Best bet is to message.To.Add() each of your MailAddresses individually. I think early versions of .Net were happier to parse apart comma or semicolon separated email addresses than the more recent runtime versions.
I was having the same problem.
The code is actually
message.To.Add("xxx#gmail.com, yyy#gmail.com");
this one can work in .net 3.5
if you use
message.To.Add( new MailAddress("xxx#gmail.com, yyy#gmail.com"));
this won't work in .net 3.5

How to add custom headers to the System.Net.Mail SMTP class?

I have a SMTP server that only accepts a predefined From sender.
However, I can add a custom from header in the DATA structure to set another from (sender ) address. This is possible if I test using Telnet to compose an email message:
>helo there
>mail from:the.only.allowed.sender#mydomain.com
>rcpt to:magnus#mydomain.com
>data
From:magnus#mydomain.com
To:some.user#mydomain.com
Subject:Test
Test message
.
When this email has arrived at the recipient, the from address is magnus#mydomain.com, which is the goal.
Here's my problem.
How can I mimic this "from header" in the System.Net.Mail SMTP class?
Setting the from property fails, because that would violate the SMTP server policies.
Something like this would be great, but it doesn't work:
var fromAddress = new MailAddress("the.only.allowed.sender#mydomain.com");
var toAddress = new MailAddress("user#mydomain.com");
string subject = "Subject";
string body = "Body";
var smtp = new SmtpClient
{
Host = "my-smtp-server",
Port = 25,
DeliveryMethod = SmtpDeliveryMethod.Network
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
ReplyTo = new MailAddress("magnus#mydomain.com"),
})
{
message.Headers.Add("From", "magnus#mydomain.com"); // <---- This would be great, if it worked
smtp.Send(message);
}
Has anybody got any ideas?
PS. Writing a custom SMTP class myself, using TCP sockets, it works, but can this be done in the standard .NET classes?
Well, I should have done some experimenting before posting the question...
(But instead of deleting it, I'll leave it here if others would have the same issue).
The solution was to set both the From and Sender properties on the MailMessage object.
(I'd need to set both, otherwise it doesn't work):
var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
From = new MailAddress("magnus#mydomain.com"),
Sender = new MailAddress("the.only.allowed.sender#mydomain.com")
};
smtp.Send(message);

Categories

Resources