Send MailGun email with Attachment from Google Drive in C# - c#

I can send emails with attachments from my local computer just fine but can't figure out how to send attachments of files that are in Google Drive.
Can the file be send with a simple path in the below code if sharing permissions are set? If so, how do I get that path?
How can I send a file without sharing permissions?
Here is my MailGun Send code...
public static void Send()
{
RestClient client = new RestClient();
client.BaseUrl = new Uri("https://api.mailgun.net/v3");
client.Authenticator = new HttpBasicAuthenticator("api", "abc123");
RestRequest request = new RestRequest();
request.AddParameter("domain", "mail.dealopo.ly", ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", "Name <test#sandboxcabc123.mailgun.org>");
request.AddParameter("to", "myemail#msn.com");
request.AddParameter("subject", "Hello");
request.AddParameter("text", "Testing some Mailgun awesomness!");
request.AddParameter("html", "<html>Inline image here: <img src=\"cid:Capture.jpg\"></html>");
request.AddFile("inline", Path.Combine(#"C:\Users\Jeremy\Desktop", "Capture.jpg"));
request.AddFile("attachment", Path.Combine(#"C:\Users\Jeremy\Desktop", "Local File.pdf"));
request.AddFile("attachment", Path.Combine(#"WHAT PATH DO I USE??", "Google Drive File.pdf"));
request.Method = Method.POST;
var response = client.Execute(request);
}

Related

mailgun api didn't throw any exception neither it's sending any mails

I am trying to send mails through C# RestClient and it didn't give any errors and when I open my mail my inbox is still empty.
RestClient client = new RestClient("https://api.mailgun.net/v2");
client.Authenticator =
new HttpBasicAuthenticator("api",
APIkey);
RestRequest request = new RestRequest();
request.AddParameter("domain",
domain, ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", from);
request.AddParameter("to", email);
request.AddParameter("subject", subject);
request.AddParameter("text", txt);
request.AddParameter("html", html);
It's give me statusCode OK, Content-Length 23. I tried to change the v2 to v3 but doesn't change anything. When I try to put domain it doesn't work. I am currently setting empty string to domain.
Please someone check this code. Any other way to check why it's not working.
Thanks

RestSharp Unable to connect to remote server

I am trying to use RestSharp in a console application, to connect to an api and get a cookie, but in the response, i keep getting "Unable to connect to remote server".
var client = new RestClient("http://finans-dk.pronestor.com/Api.mvc/v1/Authenticate");
client.Authenticator = new SimpleAuthenticator("login", "????", "password", "????");
var request = new RestRequest("resource", Method.GET);
IRestResponse response = client.Execute(request);
I basically expect RestSharp to call the following:
finans-dk.pronestor.com/Api.mvc/v1/Authenticate?login=???&password=???
This works for me in postman, but not in restsharp.
I have tested with http://ip.jsontest.com, to test if i can connect to any outside apis, this works and get my ip back.
Any ideas???
Change your code to this:
var client = new RestClient("https://finans-dk.pronestor.com/Api.mvc/v1/");
client.Authenticator = new SimpleAuthenticator("login", "????", "password", "????");
var request = new RestRequest("Authenticate", Method.GET);
IRestResponse response = client.Execute(request);
You didn't look at the documentation you linked to properly. In its example the URL is:
http://example.com/resource?username=foo&password=bar
Pay attention to where resource appears. Your URL shows Authenticate in that place instead.

Mailgun sending attachment with RestSharp

I'm using RestSharp to try and send an attachment with the Mailgun API. I have tried attaching from both a file in the system using a hardcoded path and also from a binary file stored in the database using ToArray() method on the varbinary(MAX) (SQL Server) property both with no success.
The attachment technically sends, but when the email arrives in my inbox the file size is always roughly 302bytes big and is always corrupt. I have tried 3 different files and get the same problem each time.
The rest of the email sends, delivers and displays fine. It's just the attachments that are broken.
Breakdown of code:
// Doesnt work(Data property is varbinary(MAX)
request.AddFileBytes("attachment",databaseModel.Data.ToArray(),databaseModel.Filename, "multipart/form-data");
// Also doesnt work(Data property is varbinary(MAX)
request.AddFile("attachment",databaseModel.Data.ToArray(),databaseModel.Filename, "multipart/form-data");
// Also doesnt work
var path = #"D:\Template.pdf";
request.AddFile("attachment",path,"multipart/form-data");
This code works:
public static void Main(string[] args)
{
Console.WriteLine(SendSimpleMessage().Content.ToString());
Console.ReadLine();
}
public static IRestResponse SendSimpleMessage()
{
var path1 = #"C:\Users\User\Pictures\website preview";
var fileName = "Learn.png";
RestClient client = new RestClient();
client.BaseUrl = new Uri("https://api.mailgun.net/v3");
client.Authenticator =
new HttpBasicAuthenticator("api",
"key-934345306fead7de0296ec2fb96a143");
RestRequest request = new RestRequest();
request.AddParameter("domain", "mydomain.info", ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", "Excited User <example#mydomain.info>");
request.AddParameter("to", "peter.cech#gmail.com");
request.AddParameter("subject", "Hello");
request.AddParameter("text", "Testing some Mailgun awesomness! This is all about the text only. Just testing the text of this email.";
request.AddFile("attachment", Path.Combine(path1,fileName));
request.Method = Method.POST;
return client.Execute(request);
}
I figured it out..
Not supposed to add "multipart/form-data" on the request.AddFile();
Removing this fixes the problem.

Register Endpoint on Cisco ISE with RestSharp

I'm trying to register(POST) an Endpoint on an Cisco ISE 1.3 via the RestSharp Client in a C# Console Application.
I already got it working with GET requests.
The code i used:
String XML = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> <ns3:endpoint name='name' id='id' description='Desc' xmlns:ns2='ers.ise.cisco.com' xmlns:ns3='identity.ers.ise.cisco.com'> <groupId>04f3c120-f42f-11e2-bd54-005056bf2f0a</groupId> <mac>00:00:CC:Ac:BB:CC</mac> <profileId>576bf7b0-f42f-11e2-bd54-005056bf2f0a</profileId><staticGroupAssignment>true</staticGroupAssignment> <staticProfileAssignment>true</staticProfileAssignment> </ns3:endpoint> ";
var client = new RestClient();
client.BaseUrl = new Uri("https://" + ip + ":9060/ers/config/endpoint");
client.Authenticator = new HttpBasicAuthenticator(user, pw);
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Accept", "application/vnd.com.cisco.ise.identity.endpoint.1.0+xml");
request.AddHeader("Content-Type", "application/vnd.com.cisco.ise.identity.endpoint.1.0+xml; charset=utf-8");
request.AddBody(XML);
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer.ContentType = "application/vnd.com.cisco.ise.identity.endpoint.1.0+xml; charset=utf-8";
var response = client.Execute(request);
When I submit the Code I receive a "Unsuported Media-Type" error.
The Headers are taken from the SDK from Cisco.
I already realized it with curl, SharePoint and SC Orchestrator.
I think that there is a mistake in the combination of the XML to the request but I can't find it.
Any ideas?
You need to add the content type in the headers, it is the same as the accept one:
request.Method = Method.GET; request.AddHeader("Accept",application/vnd.com.cisco.ise.identity.guestuser.2.0+xml"); request.AddHeader("Content-Type","application/vnd.com.cisco.ise.identity.guestuser.2.0+xml");

Attaching PDF file in email

I need to attach an receipt in the email which has been generated dynamically. I'm unable to attach the pdf file, it denotes me that path is not valid.
Here is my code:
public static IRestResponse SendConfirmationEmail(string emailaddress,string subject,string body)
{
RestClient client = new RestClient();
client.BaseUrl = "https://123456";
client.Authenticator = new HttpBasicAuthenticator("api", "key-abcdef12345huj");
RestRequest request = new RestRequest();
request.AddParameter("domain", "abc.com", ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", "abc <abc#xyz.com>");
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("~/users/Receipts/abc-414.pdf");
request.AddParameter("attachment",attachment);
request.AddParameter("c", emailaddress);
request.AddParameter("to", emailaddress);
request.AddParameter("subject", subject);
request.AddParameter("html", body);
request.Method = Method.POST;
return client.Execute(request);
}
Can any one help me out on this issue?
Thank you
try in this way
attachment = new System.Net.Mail.Attachment(HttpContext.Current.Server.MapPath("~/users/Receipts/abc-414.pdf"));
use Server.MapPath method that returns the physical file path that corresponds to the specified virtual path on the Web server.
I see that you are sending these parameters in a POST request. Did you check the process of serializing-deserializing? maybe there is something wrong in this step and that parameter is ignored

Categories