How can I include multiple recipients in a gmail email message? - c#

I've got this working code to send an email using my gmail account:
public static void SendEmail(string fullName, string toEmail, string HH, string HHEmailAddr)
{
var fromAddress = new MailAddress(FROM_EMAIL, FROM_EMAIL_NAME);
var toAddress = new MailAddress(toEmail, fullName);
var toAddressHH = new MailAddress(HHEmailAddr, HH);
string fromPassword = GMAIL_PASSWORD;
List<String> htmlBody = new List<string>
{
"<html><body>",
. . .
"</body></html>"
};
var body = string.Join("", htmlBody.ToArray());
var smtp = new SmtpClient
{
Host = "smtp.gmail.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 = body,
IsBodyHtml = true
})
{
smtp.Send(message);
}
}
The problem is that I want to send the email to two recipients, not one. I can theoretically add another message to the end of that code like so:
. . .
using (var messageHH = new MailMessage(fromAddress, toAddressHH)
{
Subject = subject,
Body = body,
IsBodyHtml = true
})
{
smtp.Send(messageHH);
}
}
...sending two emails from one code block, but what I really want to do is something like this:
List<MailAddress> recipients = new List<MailAddress>();
recipients.Add(toAddress);
recipients.Add(toAddressHH);
. . .
using (var message = new MailMessage(fromAddress, recipients)
...but there seems to no such overload for MailMessage's constructor. How can I add a second recipient to the sending of an email from gmail? Both as a co-recipient and as a "CC" recipient would be nice to know.
UPDATE
If I do try the suggested:
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
To.Add("dplatypus#att.net", "Duckbilled Platypus")
})
...I get:
Invalid initializer member declarator
..and:
The name 'To' does not exist in the current context
I get the same with the following permutation:
To.Add(new MailAddress("dplatypus#att.net", "Duckbilled Platypus"))

Looking at Microsoft's Documentation, you can see that the MailMessage.To property is a MailAddressCollection.
The MailAddressCollection has an Add() method that will append values to the collection.
With that information, you can try something like this:
messageHH.To.Add(new MailAddress("recipient1#domain.com","Recipient1 Name"));
messageHH.To.Add(new MailAddress("recipient2#domain.com","Recipient2 Name"));
messageHH.To.Add(new MailAddress("recipient3#domain.com","Recipient3 Name"));
//etc...

I had to change the "style" of declaration, but this works:
var message = new MailMessage(fromAddress, toAddress);
message.Subject = subject;
message.Body = body;
message.To.Add(new MailAddress("dplatypus#att.net", "Duckbilled Platypus"));
message.To.Add(new MailAddress("duckbill#att.net", "Platypus 2"));
smtp.Send(message);

Related

Send Email from Alias Address using SMTP

This question has been asked so many times but I am still struggling to find a working solution.
Please consider the below code:
SmtpClient mailClient = new SmtpClient("outlook.office365.com");
MailMessage msgMail = new MailMessage();
msgMail.From = new MailAddress("validUser#domain.com", "displayName#aliasDomain.com");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = new NetworkCredential("validUser#domain.com", "password");
mailClient.EnableSsl = true;
MailAddress sendMailTo = new MailAddress("someValidUser#someValidDomain.com", "Mark Twain")
msgMail.To.Add(sendMailTo);
msgMail.Subject = "Test Subject";
msgMail.Body = "Email content";
msgMail.IsBodyHtml = true;
mailClient.Send(msgMail);
msgMail.Dispose();
When someValidUser - the recipient - receives the email, I want it to show the display name : displayName#aliasDomain.com as opposed to the username registered to the validUser#domain.com account.
How can I achieve this?
Try adding the display name to the headers of the message:
msgMail.Headers.Add("Sender", "displayName#aliasDomain.com");
I hope this helps.
If you reset the from address in the MailMessage object you should be able to send from the alias email address. See below for the full code.
var fromAddress = new MailAddress("root#bobloblaw.com", "Root");
var toAddress = new MailAddress("plaintiff#gmail.com", "Plaintiff");
string fromPassword = "password";
string subject = "Your Lawsuit";
string body = "I regret to inform you...";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
Timeout = 20000
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body,
From = new MailAddress("bob#bobloblaw.com", "Bob Lob")
})
{
smtp.Send(message);
}
This can work if you simply add an Alias to the Office 365 mailbox. Then use the Alias in the From field and the original username+password for the Credentials.
The recipients will see the name+alias you specify in the From field and not the username for the account. Just tried this with PowerShell Send-MailMessage as well as C#.

How attach mail template in .NET core

I am trying to attach the mail template in Mail Kit I am using .NET Core
here is my code
//From Address
string FromAddress = "info#gorollo.com";
string FromAdressTitle = "Gorollo";
//To Address
string ToAddress = email;
string Subject = subject;
string BodyContent = message;
var body = new BodyBuilder();
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress
(FromAdressTitle,
FromAddress
));
mimeMessage.To.Add(new MailboxAddress
(
ToAddress
));
mimeMessage.Subject = Subject;
using (var client = new SmtpClient())
{
client.Connect("smtp.mailgun.org", 587, false);
client.Authenticate(
"info#gorollo.com",
"password"
);
await client.SendAsync(mimeMessage);
await client.DisconnectAsync(true);
}
i saw many examples but not able to find the exact solution
so please anyone help me to sort out the problem
I have to attach Signup template in my mail here.
You're almost here. You can use the BodyBuilder to add text or HTML to the body. Either use the TextBody or HtmlBody property to set text or HTML respectively. Use the ToMessageBody() method to create a MimeEntity instance which you can use as the mail body.
Example:
var message = new MimeMessage();
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "<h1>Hello, World!</h1>";
message.Body = bodyBuilder.ToMessageBody();

Email to Text not working

I am trying to send an email as text to end users mobile, the code is running fine but I can't seems to get the SMS.
Here is my code
public Task SendAsync()
{
var fromAddress = new MailAddress("test#gmail.com", "Authenticator");
var toAddress = new MailAddress("mynumber#sms.sancharnet.in", "User");
const string fromPassword = "mypassword";
const string subject = "subject";
string body = Session["PIN"].ToString();
var smtp = new SmtpClient
{
Host = "smtp.gmail.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 = body,
IsBodyHtml = false
})
{
smtp.Send(message);
}
return Task.FromResult(0);
}
Is there something I am missing here ? or is this not how we try to do an Email to text ?

C# WPF - How to send an e-mail (to myself)?

for debugging purposes, I have globally handled all exceptions. Whenever an exception occurs, I silently handle it, and want to send myself an e-mail with the error details, so I can address this issue.
I have two emails, email1#gmail.com, and email2#gmail.com...
I have attempted using this code to send myself an e-mail, but it is not working.
string to = "email1#gmail.com";
string from = "email2#gmail.com";
string subject = "an error ocurred";
string body = e.ToString();
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.google.com");
client.Timeout = 100;
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
I have tried countless other pieces of code but I have no idea how to do it. Does anyone have a solid solution for this? Thanks a bunch.
This has to work. See more info here: Sending email in .NET through Gmail
using System.Net;
using System.Net.Mail;
//...
var fromAddress = new MailAddress("alextodorov01#abv.bg", "From Name");
var toAddress = new MailAddress("kozichka01#abv.bg", "To Name");
const string fromPassword = "fromPassword";
const string subject = "an error ocurred";
const string body = e.ToString();
var smtp = new SmtpClient
{
Host = "smtp.gmail.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 = body
})
{
smtp.Send(message);
}
//...

c# sending mail pass string to body

I was following this: post
And after a few seconds realized that the body is a constant and I can't pass a string to it. Is there any fast way to change this code a bit and get what I need?
public void PostMessage(string body,string subject)
{
var fromAddress = new MailAddress("from#gmail.com", "From Name");
var toAddress = new MailAddress("to#example.com", "To Name");
const string fromPassword = "fromPassword";
var smtp = new SmtpClient
{
Host = "smtp.gmail.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 = body,
})
{
smtp.Send(message);
}
}
you can call it like this:
PostMessage("MAH BODY", "SUBJECT");
You remove the const bit...
const string body = "Body";
turns into:
string body = bodyPassedIn; //where bodyPassedIn = is being passed into the method
You don't even need the variable at all:
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = bodyPassedIn // here!
})
{
smtp.Send(message);
}

Categories