Attaching PDF file in email - c#

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

Related

The value cannot be null or empty. (Parameter 'mediaType')

I am getting an exception 'The value cannot be null or empty. (Parameter 'mediaType')' when trying to send an attachment using the RestSharp library. For email sending, I am using MailGun api and the solution is on .NET 7 framework
var client = new RestClient(new Uri(BaseUrl))
{
`Authenticator = new HttpBasicAuthenticator("api", ApiKey)
};
var request = new RestRequest();
request.AddParameter("domain", domain, ParameterType.UrlSegment);
request.Resource = $"{domain}/messages";
request.AddParameter("from", "noreply#smartbroker.com");
request.AddParameter("to", "ahuja.dinesh85#gmail.com");
request.AddParameter("subject", "test subject");
request.AddParameter("html", "hello");
//Attachment
const string fileName = "D:\\ebook.pdf";
request.AddFile("attachment", fileName);
request.Method = Method.Post;
var response = await client.ExecuteAsync(request);
If I comment the attachment code the email goes fine. Do you have any idea what I am doing wrong?
Thanks in advance
It seems there is a problem with the version of RestSharp. I couldn't fix that problem, but it works fine on .net 6 and earlier.
I hope it has helped you, greetings!

Send MailGun email with Attachment from Google Drive in 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);
}

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

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");

Categories