Sending email using CDO - c#

I have a database which contains email address of recipients and a flag column. If the flag is "YES", a mail will be sent to that user. I want to accomplish this using CDO in my ASP.NET project. So, what may be the favorable routine for doing this. I am new to CDO.
What I tried as a beginner is this:
CDO.Message oMsg = new CDO.Message();
CDO.IConfiguration iConfg;
iConfg = oMsg.Configuration;
ADODB.Fields oFields;
oFields = iConfg.Fields;
ADODB.Field oField = oFields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
oFields.Update();
oMsg.Subject = "Test CDO";
oMsg.From = "abc#xyz.com";
oMsg.To = "someone#example.com";
//oMsg.TextBody = "CDO Mail test";
oMsg.HTMLBody = "CDO Mail test";
oMsg.Send();
Response.Write("Mail Sent");
But its not helping me what I want.

Related

Gmail recipients are not getting emails sent using C#

I had an working email sending functionality using c#. Suddenly gmail recipients are not getting the mails(neither in inbox nor spam). But other recipients (like outlook or any domain) are getting mails in their inbox.
Here is my code:
SmtpClient SmtpMail = new SmtpClient();
MailMessage myMail = new MailMessage();
System.Text.Encoding myEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");
myMail.SubjectEncoding = myEncoding;
myMail.BodyEncoding = myEncoding;
myMail.From = (new MailAddress("Administrator#domain.com", "User Name"));
myMail.To.Add("somemail#gmail.com");
myMail.Subject = "Subject";
myMail.Priority = MailPriority.High;
SmtpMail.Port = 587;
myMail.IsBodyHtml = true;
myMail.Body = "<h1>Hi This is robin</h1>";
SmtpMail.Host = "mail17.ezhostingserver.com";
SmtpMail.EnableSsl = true;
SmtpMail.UseDefaultCredentials = false;
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("useremail", "password");
SmtpMail.Credentials = basicAuthenticationInfo;
SmtpMail.Send(myMail);
However, I tried to manually send message from their Mail Application to a Gmail user. In that case the recipient is getting the mail. Mails only sent from the code to any gmail user is the main problem here.
NB: I have already tried port:26, SmtpMail.UseDefaultCredentials = true. Still no luck
In my case, google was considering my emails as spammy and blocked the emails for reducing the amount of spam sent to Gmail.
Please visit https://support.google.com/mail/?p=UnsolicitedMessageError for more information

Can clients send email to each other via your website?

I am working on an online shopping website. I want the users directly contact each other via emails by using a web form. I have tried different ways. but it didn't work.
I was also trying to use the gmail smtp as follow:
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.UseDefaultCredentials = true;
client.Credentials = new NetworkCredential("example#gmail.com", "mypassword");
client.EnableSsl = true;
string fromAddress = "buyer#gmail.com";
string messageSubject = "Your Subject";
string body = "Content";
var supEmail = "seller#yahoo.com";
MailMessage mm = new MailMessage(fromAddress, supEmail, messageSubject, body);
MailAddress copy = new MailAddress("notifications#mydomain.com");
mm.CC.Add(copy);
//Set up your encoding
mm.BodyEncoding = UTF8Encoding.UTF8;
mm.Priority = System.Net.Mail.MailPriority.High;
//Send your message
client.Send(mm);
Any idea or suggestions?
This won't work. Google won't allow you to send an email from an account you don't own... so if you want to send an email via Google then it needs to be from either the Google email account your are validating with or associated with that account.

Not receiving email using SendGrid

I am attempting to send an email using the SendGrid library: https://github.com/sendgrid/sendgrid-csharp
More specifically I am doing this...
// Create the email object first, then add the properties.
SendGridMessage myMessage = new SendGridMessage();
myMessage.AddTo("anna#example.com");
myMessage.From = new MailAddress("john#example.com", "John Smith");
myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";
// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("username", "password");
// Create an Web transport for sending email.
var transportWeb = new Web(credentials);
// Send the email.
transportWeb.DeliverAsync(myMessage);
The code runs fine however I never receive an email!! How do I diagnose this problem?
Note the email doesn't appear in the Bounces/Blocks/Spam Reports or Invalid Email lists.
I know that this is an old post, but here's my answer:
SendGrid is not used for receiving emails to your email address, only to send them in mass. If you want to use I suggest using SmtpClient from MailKit. Be sure to use the proper hostname, like smtp.gmail.com, a port number, like 465 for SSL, and the proper credentials (Username and password of any email address).
One thing to note, if you use your own credentials for your own email address and you want to send it to that same email address, like if you want to send an email to JoeBlank#email with it's own credentials, you will only send to yourself. If you want to reply back to a different email address, fill in the ReplyTo field. The idea is to let the email of the authorized email address send you the message. Here's the code:
using MimeKit;
using MailKit.Net.Smtp;
MimeMessage message = new MimeMessage();
message.From.Add(new MailboxAddress("Sender Name", "sender#example.com"));
message.To.Add(new MailboxAddress("Receiver Name", "receiver#example.com"));
message.ReplyTo.Add(new MailboxAddress("ReplyTo Name", "replyto#example.com"));
message.Subject = "Subject";
message.Body = new TextPart("plain")
{
Text = plainTextContent
};
using (SmtpClient smClient = new SmtpClient())
{
smClient.Connect("smtp.gmail.com", 465, true);
smClient.Authenticate("user", "pass");
await smClient.SendAsync(message);
smClient.Disconnect(true);
}

Send Email using SMTP in C# + Template Processing Mail Merge

Currently i am using SMTP Client to send email in C#.
I have one email template defined in which i replace the value at run time.
MailDefinition mailDefinition = new MailDefinition();
mailDefinition.BodyFileName = "~/Email-Templates/File.html";
mailDefinition.From = "abc#gmail.com";
//Create a key-value collection of all the tokens you want to replace in your template...
ListDictionary replacements = new ListDictionary();
replacements.Add("<%FirstName%>", "abc");
replacements.Add("<%LastName%>", "xyz");
replacements.Add("<%ManagerName%>", "Tom");
replacements.Add("<%Address1%>", "USA");
replacements.Add("<%Address2%>", "USA");
replacements.Add("<%City%>", "uk");
replacements.Add("<%State%>", "uk");
replacements.Add("<%Zip%>", "9876543");
string mailTo = string.Format("{0} {1} <{2}>", "abc", "xyz", "abc#gmail.com");
MailMessage mailMessage = mailDefinition.CreateMailMessage(mailTo, replacements, this);
mailMessage.From = new MailAddress("abc#gmail.com", "test site");
mailMessage.IsBodyHtml = true;
mailMessage.Subject = "User Details";
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("abc#gmail.com", "aaaaaaaa"),
EnableSsl = true
};
client.Send(mailMessage);
Template looks like this :
Hello <%FirstName%> <%LastName%>,
Thank you for creating an account with us.
Here are your details:
Your Manager is <%ManagerName%>
<%Address1%>,
<%Address2%>
<%City%>, <%State%> <%Zip%>
Thank You,
abc
Now my question is in future i want to add newfield to the email template , i need to add the same to my code and rebuild the system, this is what i dont want , i dont want to build my system everytime.
Is there a way or some other approach where i need not to build the system everytime ?
I can configure this from some where else ?
Thanks.

C# Outlook Interop Send from Folder

I'm attempting to send an email from a e-mail address that's listed as a folder. Basically I have a folder with an e-mail address assigned to it. Whenever something comes to that email it goes to the folder. The E-Mail Address is not an account assigned to me. I would use SMTP But our corporate network does not allow this.
How can I send an e-mail in C# from this Folder's E-Mail?
My code is setup as follows.
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, true, true);
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.Subject = subject;
string html;
html = message;
html = html.Replace("\n","<br/>");
oMsg.HTMLBody = html;
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(to);
//Rest of my closing stuff here.
If you already have the folder's email address (you don't mention if this is part of the problem but it sounds like it is not) you shouldn't have to use Outlook interop for this. Try the classes in System.Net.Mail. This site has some good examples, but here's something quick:
const string PR_SMTP_ADDRESS =
"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
...
var msg = new MailMessage();
msg.From = new MailAddress(recipient.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS).ToString());
msg.To.Add(new MailAddress(folderAddress));
msg.Subject = subject;
msg.IsBodyHtml = true;
msg.Body = html;
var smtpClient = new SmtpClient("{SMTP server address or IP}");
smtpClient.Send(msg);
I'm only guessing about the part where I get the recipient address, it's based on this MSDN page.
Seems to me the whole folder thingy is irrelevant to your problem (correct me if I'm wrong there), and all it comes down to is you want to send an e-mail through Outlook with a specific reply address. You can use MailItem.SenderEmailAddress for that purpose:
oMsg.SenderEmailAddress = "my.special.address#domain.com"
As an alternative, you could add the reply address to the MailItem.ReplyRecipients collection.

Categories