Exchange WebService sends only plain Text through c# - c#

I have a program that sends eMail through the Exchange Webservice:
//EXCHANGE API
using Microsoft.Exchange.WebServices;
using Microsoft.Exchange.WebServices.Data;
//BODY
string mBody = "<strong>TEXT</strong> Test";
//SETUP
ExchangeService service = new ExchangeService();
service.Url = new Uri("https://"+uriVar);
service.Credentials = new WebCredentials(userNameVar, passwordVar);
EmailMessage message = new EmailMessage(service);
message.Subject = mSubject;
message.Body = new MessageBody(BodyType.HTML,mBody);
message.ToRecipients.Add(recipient);
message.Save();
//Adding Image(s)
string fileFacebook = "facebook.png";
message.Attachments.AddFileAttachment("facebook.png", fileFacebook);
message.Attachments[0].IsInline = true;
message.Attachments[0].ContentId = "imgFacebook";
//Send
message.Send();
(the image is added in the original body, not in this example)
However, the eMail goes through as plain text, I cannot find out why. I am pretty sure it worked some time ago, is it possible that something has been changed on the Server? If so, what could it be?

Related

getting "Unknown Mailbox" error when sending email using EWS API (C#)

I'm simply trying to use the Office 365 API to send an email via the "Send()" function, but am getting back Microsoft.Exchange.Webservices.Data.ServiceResponseException: Mailbox does not exist.
Here's my exchange service:
_emailExchangeService =
new ExchangeService(ExchangeVersion.Exchange2013_SP1)
{
Url = new Uri(_settings.ExchangeWebServiceEndpoint),
Credentials = new WebCredentials(_settings.AppEmailUserName, _settings.AppEmailPassword),
TraceEnabled = true,
UseDefaultCredentials = false
};
And here's the code I'm using to send the email:
public void SendEmail(MemoryStream attachment, string body, string subject, string recipients, string fromMailbox)
{
EmailMessage message = new EmailMessage(_emailExchangeService);
message.From = fromMailbox;
message.Subject = subject;
message.Body = new MessageBody(BodyType.Text, body);
message.ToRecipients.Add(recipients);
message.Attachments.AddFileAttachment("FileName", attachment);
message.Send();
}
What mailbox am I forgetting to define when sending this, the sent box? I thought the "from" field would define the mailbox for sending items. I'm just not even sure where to do that and my code looks identical to the docs.
Side note: I know the exchange service is set up correctly because if I define an inbox email address and attempt to FindItems(_inbox) on the mailbox, it works.

Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email .

The official Gmail API documentation is horrendous. Not getting any clue to integrate Gmail API using .NET framework in vs2017. I wanted to send the input data of the Web form to a user's email.
It would be a three step process -
Define an HTML template which which describes how your mail should be presented.
Write a small c# code to replace all place holders like your form fields , user name, etc.
private string createEmailBody(string userName, string title, string message)
{
string body = string.Empty;
//using streamreader for reading my htmltemplate
using(StreamReader reader = new StreamReader(Server.MapPath("~/HtmlTemplate.html")))
{
body = reader.ReadToEnd();
}
body = body.Replace("{UserName}", userName); //replacing the required things
body = body.Replace("{Title}", title);
body = body.Replace("{message}", message);
//// Instead of message add you own parameters.
return body;
}
When form is submitted, call step 2 code first. Then use it's output to set mail body.
Code would be something like -
string smtpAddress = "smtp.gmail.com";
int portNumber = 587;
bool enableSSL = true;
/// This mail from can just be a display only mail I'd
string emailFrom = "no-reply#gmail.com";
string subject = "your subject";
string body = createEmailBody();
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
/// Add more to IDs if you want to send it to multiple people.
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = true;
// This is required to keep formatting of your html contemts
/// Add attachments if you want, this is optional
mail.Attachments.Add(new Attachment(yourfilepath));
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(your-smtp-account-email, your-smtp-account-password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
Refer this link for working example
https://www.c-sharpcorner.com/UploadFile/33b051/sending-mail-with-html-template/
EDIT: For using GMail API
Using GMAIL APIs you will need two nuget packages:
1. Install-Package Google.Apis.Gmail.v1
2. Install-Package AE.Net.Mail
Code is very similar to what we have for normal SMTP mail send. It is explained at: http://jason.pettys.name/2014/10/27/sending-email-with-the-gmail-api-in-net-c/

Add list-unsubscribe header with Amazon SES

I'm searching for an working C# example to add list-unsubscribe header with Amazon SES.
After reading that Amazon-SES now supports adding Headers I was searching for an C# example but was unable to find one.
I couldn't find nice API like they have for Java. For C#, I found two alternative.
The easiest option is probably to switch to the SMTP interface and .Net's native SMTP classes (or a third-party library): Send an Email Using SMTP with C#
The example code is using the MailMessage class from System.Net.Mail:
// Create and build a new MailMessage object
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(FROM,FROMNAME);
message.To.Add(new MailAddress(TO));
message.Subject = SUBJECT;
message.Body = BODY;
// Comment or delete the next line if you are not using a configuration set
message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);
Another (less attractive) option is to use SendRawEmailRequest. With this API, you have to encode the message along with its headers, attachments, and other data, in a MemoryStream.
Example code, from the AWS SDK .Net documentation - SES - RawMessage:
var sesClient = new AmazonSimpleEmailServiceClient();
var stream = new MemoryStream(
Encoding.UTF8.GetBytes("From: johndoe#example.com\n" +
"To: janedoe#example.com\n" +
"Subject: You're invited to the meeting\n" +
"Content-Type: text/plain\n\n" +
"Please join us Monday at 7:00 PM.")
);
var raw = new RawMessage
{
Data = stream
};
var to = new List<string>() { "janedoe#example.com" };
var from = "johndoe#example.com";
var request = new SendRawEmailRequest
{
Destinations = to,
RawMessage = raw,
Source = from
};
sesClient.SendRawEmail(request);

Mail body: & becomes (R)

We have a site from where we send a auto generated mail to our customer when a new customer register. our mail got a activation link which look like
http://www.bba-reman.com/catalogue/ConfirmRegistration.aspx?email=meyer#reman.de&id=907a5253-106c-4fb3-9882-83e634e651b2
but when our german customer reveive the mail then he got the below activation link where you notice & character change to ®
http://www.bba-reman.com/catalogue/ConfirmRegistration.aspx?email=meyer#reman.de®id=907a5253-106c-4fb3-9882-83e634e651b2
Can anyone tell me why & character getting change to ®?
How to resolve this kind of problem?
try using CreateAlternateViewFromString property, here is the example code
MailMessage emailmsg = new MailMessage("from#address.co.za", "to#address.co.za")
emailmsg.Subject = "Subject";
emailmsg.IsBodyHtml = false;
emailmsg.ReplyToList.Add("from#address.co.za");
emailmsg.BodyEncoding = System.Text.Encoding.UTF8;
emailmsg.HeadersEncoding = System.Text.Encoding.UTF8;
emailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
emailmsg.Body = null;
var plainView = AlternateView.CreateAlternateViewFromString(EmailBody, emailmsg.BodyEncoding, "text/plain");
plainView.TransferEncoding = TransferEncoding.SevenBit;
emailmsg.AlternateViews.Add(plainView);
SmtpClient sSmtp = new SmtpClient();
sSmtp.Send(emailmsg);

Send Email to external domain asp.net

I'm unable to send any email to an external email address from my ASP.NET webpage.
Below is my code:
Dim oMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
Dim add As New MailAddress("emailaddy#external_address.com")
oMsg.From = New MailAddress("something#mydomain.com")
oMsg.To.Add(add)
oMsg.Subject = "Your Account Details & Information"
oMsg.Body = "my body text"
oMsg.IsBodyHtml = True
Dim smtp As New System.Net.Mail.SmtpClient("local_smtp_server", 25)
smtp.DeliveryMethod = SmtpDeliveryMethod.Network
smtp.Credentials = New System.Net.NetworkCredential("auser#mydomain.com", "mypassword")
smtp.Send(oMsg)
is there anything I am missing? If I send to a local email it will work just fine. Is there something that I need to change on the MS Exchange Server?
Any code samples can be given in C# as well...

Categories

Resources