How to send existing email from inbox to other email address using graph API c#
I have tried sending the existing email but Im not able to send it.
It is throwing error -Code: generalException Message: Unexpected exception returned from the service.
Below is the code snippet:
internal static async Task<System.Collections.Generic.IEnumerable<Message>> Sendemail(List<MM_Mailbox_Forward> toAddress, string smptpadd)
{
string from ="xyz#gmail.com"``your text``
var Toptenmessages1 = graphClient.Users["xyz#gmail.com"].MailFolders["Inbox"].Messages.Request().Top(1);
var task = System.Threading.Tasks.Task.Run(async () => await Toptenmessages1.GetAsync());
var authResult2 = task.Result;
foreach (var message in authResult2)
{
try
{
var messages = new Message
{
Subject = message.Subject,
Body = message.Body,
ToRecipients = new List<Recipient>()
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "abc#gmail.com"
}
}
},
};
await graphClient
.Users[from]
.SendMail(messages, true)
.Request()
.PostAsync();
}
}
catch (Exception ex)
{
Console.WriteLine("error:" + ex);
}
Make sure you are not using a shared email box, you can't (by default) send email with a shared email box.
I have created one AWS Lambda function using Visual Studio and C# with .net core1.0
I have to send an email after doing some operation on my Lambda function.
My code is as below. taken from AWS Doc
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.Lambda.Core;
using System;
using System.Collections.Generic;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using Amazon;
namespace MyProject
{
public class AddAlarms
{
private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
public string AddAlarmsHandler(GetUserAlerts input, ILambdaContext context)
{
var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1);
List<string> toAddress = new List<string>();
toAddress.Add("ToEmail#domain.com");
var sendRequest = new SendEmailRequest
{
Source = "sourceEmail#domain.com",
Destination = new Destination
{
ToAddresses = toAddress
},
Message = new Message
{
Subject = new Content("Subject of email"),
Body = new Body
{
Html = new Content
{
Charset = "UTF-8",
Data = "Hello Email, HTML Body"
},
Text = new Content
{
Charset = "UTF-8",
Data = "Hello email, Text Body"
}
}
},
ConfigurationSetName = "ConfigSet"
};
string returnval = string.Empty;
try
{
Console.WriteLine("Sending email using Amazon SES...");
returnval += "Sending email using Amazon SES...";
var response = client.SendEmailAsync(sendRequest);
System.Threading.Thread.Sleep(5000);
returnval += ", Response Status - " + response.Status.ToString() + " - ";
Console.WriteLine("The email was sent successfully.");
returnval += "The email was sent successfully.....";
}
catch (Exception ex)
{
returnval += "The email was not sent.";
Console.WriteLine("The email was not sent.");
returnval += "Error message: " + ex.Message;
Console.WriteLine("Error message: " + ex.Message);
}
return returnval;
}
}
}
so Lambda function created successfully and it shows in section of Lambda into AWS
so while testing this function i get response that after sending - "Faulted"
and email not received by receiver.
I have verified both email TO and FROM into email verification of AWS SES.
I get one Node.Js code which runs successfully and also receiving email and node.js code i have taken from link
I have checked ROLE and using same role for Node.Js code and works fine, but for C# lambda function i am getting response "Faulted".
Please help to solve this problem in AWS Lambda
Getting the following error when trying to send from local dev environment for google API. Not sure the right way to attack it. Any advice would be appreciated. Here is the Full Text of the method I am working on.
This is all Google code.
private const string ApplicationName = "<FromGoogle>";
private const string SenderEmailAddress = "<FromGoogle>";
private const string ClientId = "<FromGoogle>";
private const string ClientSecret = "<FromGoogle>";
private static GmailService _service;
private static StringWriter _message;
public static Message SendMail(string subject, string body, params string[] recipients)
{
string[] scopes = { GmailService.Scope.GmailCompose, GmailService.Scope.GmailSend };
ClientSecrets secrets = new ClientSecrets() { ClientId = ClientId, ClientSecret =
ClientSecret };
string folder = HttpContext.Current.Server.MapPath("~/App_Data") + "/gmail-credentials.json";
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets,
scopes, "user", CancellationToken.None, new FileDataStore(folder, true)).Result;
_service = new GmailService(new BaseClientService.Initializer() { HttpClientInitializer
= credential, ApplicationName = ApplicationName });
MailMessage msg = new MailMessage
{
Subject = subject,
Body = body,
From = new MailAddress(SenderEmailAddress)
};
foreach (var recipient in recipients) msg.To.Add(new MailAddress(recipient));
msg.ReplyTo.Add(msg.From);
_message = new StringWriter();
msg.Save(_message);
Message result = _service.Users.Messages.Send(new Message { Raw =
Base64UrlEncode(_message.ToString()) }, "me").Execute();
return result;
}
private static string Base64UrlEncode(string input)
{
var inputBytes = Encoding.UTF8.GetBytes(input);
// Special "url-safe" base64 encode.
return Convert.ToBase64String(inputBytes)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
I get the following exception
Exception thrown: 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException'
in Google.Apis.dll
Additional information: Error:"invalid_grant", Description:"", Uri:""
In the end the code was correct. Turns out to be an authorisation issue with Google to which Doctor Jones referred.
After working through the code I changed the ClientId to the Client email. Google specifically did not like that and brought up a web page and told me I was denied.
When I put the code back as it was I ran the test below again and it brought up a google authorisation webpage and after having gone through that the tests were working.
[TestMethod()]
public void SendMailTest()
{
string subject ="subject";
string body = "body";
string recipient = "test#test.com";
Message result = null;
try
{
result = MailTools.SendMail(subject, body, recipient);
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
result.ShouldNotBeNull();
}
I am using SendGrid mailhelper (as part of C# SDK) to send email. I need to send to multiple users, and hence I am using Personalization.
I get an error : Bad Request
This is my code:
static async Task Execute(string sub, string body, List<Recipient> recipients)
{
string apiKey = Environment.GetEnvironmentVariable("SendGrid_ApiKey", EnvironmentVariableTarget.User);
dynamic sg = new SendGridAPIClient(apiKey);
SendGrid.Helpers.Mail.Email from = new SendGrid.Helpers.Mail.Email("test1#gmail.com");
string subject = sub;
Personalization personalization = new Personalization();
SendGrid.Helpers.Mail.Email emails = new SendGrid.Helpers.Mail.Email();
var i = 0;
foreach (var recp in recipients)
{
emails.Address = recp.Email;
emails.Name = recp.FirstName + " " + recp.LastName;
personalization.AddTo(emails);
i++;
}
SendGrid.Helpers.Mail.Email to = new SendGrid.Helpers.Mail.Email("test1#gmail.com");
Content content = new Content("text/plain", body);
Mail mail = new Mail(from, subject, to, content);
mail.AddPersonalization(personalization);
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
}
I appreciate if someone could advise me what am I doing incorrect.
Sendgrid API responds with bad request when there are more than 1 email address that is the same in the Personalization object. Make sure all the emails are unique
I am trying to read emails from Gmail. I have tried every API / open source project I can find, and can not get any of them working.
Does anyone have a sample of working code that will allow me to authenticate and download emails from a Gmail account?
Final working version posted below: https://stackoverflow.com/a/19570553/550198
Using the library from: https://github.com/pmengal/MailSystem.NET
Here is my complete code sample:
Email Repository
using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;
namespace GmailReadImapEmail
{
public class MailRepository
{
private Imap4Client client;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get { return client ?? (client = new Imap4Client()); }
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
}
Usage
[TestMethod]
public void ReadImap()
{
var mailRepository = new MailRepository(
"imap.gmail.com",
993,
true,
"yourEmailAddress#gmail.com",
"yourPassword"
);
var emailList = mailRepository.GetAllMails("inbox");
foreach (Message email in emailList)
{
Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0)
{
foreach (MimePart attachment in email.Attachments)
{
Console.WriteLine("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
}
}
}
}
Another example, this time using MailKit
public class MailRepository : IMailRepository
{
private readonly string mailServer, login, password;
private readonly int port;
private readonly bool ssl;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
this.mailServer = mailServer;
this.port = port;
this.ssl = ssl;
this.login = login;
this.password = password;
}
public IEnumerable<string> GetUnreadMails()
{
var messages = new List<string>();
using (var client = new ImapClient())
{
client.Connect(mailServer, port, ssl);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(login, password);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.Not(SearchQuery.Seen));
foreach (var uniqueId in results.UniqueIds)
{
var message = inbox.GetMessage(uniqueId);
messages.Add(message.HtmlBody);
//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
}
client.Disconnect(true);
}
return messages;
}
public IEnumerable<string> GetAllMails()
{
var messages = new List<string>();
using (var client = new ImapClient())
{
client.Connect(mailServer, port, ssl);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(login, password);
// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.NotSeen);
foreach (var uniqueId in results.UniqueIds)
{
var message = inbox.GetMessage(uniqueId);
messages.Add(message.HtmlBody);
//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
}
client.Disconnect(true);
}
return messages;
}
}
Usage
[Test]
public void GetAllEmails()
{
var mailRepository = new MailRepository("imap.gmail.com", 993, true, "YOUREMAILHERE#gmail.com", "YOURPASSWORDHERE");
var allEmails = mailRepository.GetAllMails();
foreach(var email in allEmails)
{
Console.WriteLine(email);
}
Assert.IsTrue(allEmails.ToList().Any());
}
You don't need any extra 3rd Party Libraries if a summary of the 20 most recent emails is sufficient for you. You can read the data from API that Gmail has provided here: https://mail.google.com/mail/feed/atom
The response in XML format can be handled by the code below:
try {
const string emailAddress = "YourEmail";
// App Password, not password
// See: https://support.google.com/accounts/answer/185833?hl=en
const string appPassword = "YourAppPassword";
string response;
string title;
string summary;
XmlDocument xmlDocument = new XmlDocument();
HttpClient httpClient = new HttpClient();
// Logging in Gmail server to get data
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{emailAddress}:{appPassword}")));
// Reading data and converting to string
response = await httpClient.GetStringAsync(#"https://mail.google.com/mail/feed/atom");
// Remove XML namespace to simplify parsing/selecting nodes
response = response.Replace(#"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", #"<feed>");
// Loading into an XML so we can get information easily
xmlDocument.LoadXml(response);
// Amount of emails
string nr = xmlDocument.SelectSingleNode(#"/feed/fullcount").InnerText;
// Reading the title and the summary for every email
foreach (XmlNode node in xmlDocument.SelectNodes(#"/feed/entry")) {
title = node.SelectSingleNode("title").InnerText;
summary = node.SelectSingleNode("summary").InnerText;
Console.WriteLine($"> {title}");
Console.WriteLine($"{summary}");
Console.WriteLine();
}
} catch (Exception ex) {
MessageBox.Show($"Error retrieving mails: {ex.Message}");
}
Have you tried POP3 Email Client with full MIME Support ?
If you don't it's a very good example for you. As an alternativ;
OpenPop.NET
.NET class library in C# for communicating with POP3 servers. Easy to
use but yet powerful. Includes a robust MIME parser backed by several
hundred test cases. For more information, visit our project homepage.
Lumisoft
You can also try Mail.dll IMAP client.
It supports all Gmail IMAP protocol extensions:
Thread ID,
Message ID,
Labels,
Localized folder names,
Google search syntax
OAuth authentication.
Please note that Mail.dll is a commercial product, I've developed.