Is there any way to increase the ASCII length for sending a message? RRS feed

  • Question

  • I'm using System.net mail to send email via Amazon SES, The issue i'm facing here is that when i try attaching a particular Chinese file it fails, it is mostly because the length of those characters are more, and i keep getting an error as : 

    Expected parameter value, got è.

    Monday, July 30, 2018 10:11 AM

All replies

  • Would you have crafted a concise and complete example as I told you, you would see that this is nonsense, your code bails out, cause your from string is invalid:

    namespace ConsoleCS
        using System;
        using System.Net.Mail;
        using System.Text;
        class Program
            static void Main(string[] args)
                MailMessage message;
                message = CreateMailMessage("bla@bla.com");
                message = CreateMailMessage("reciever@.com"); 
            private static MailMessage CreateMailMessage(string senderAddress)
                MailMessage result = new MailMessage();            
                result.From = new MailAddress(senderAddress);
                string file = @"C:\Temp\跃科人才思想领导力活动调研.pdf";
                Attachment attachment = new Attachment(file);
                Encoding encoding = Encoding.GetEncoding("GB2312");
                attachment.NameEncoding = encoding;
                return result;

    Monday, July 30, 2018 11:16 AM
  • Hi Stefan

    I had tried this encoding earlier still no success i keep getting the same error.
    Is there any other way?


    Monday, July 30, 2018 11:23 AM
    Monday, July 30, 2018 11:34 AM
  • Hi Hani,

    What if you try encoding with GB 18030 as this is the latest encoding. Because GB2312 charset is subset of GB18030 charset.

    Encoding encoding = Encoding.GetEncoding("GB18030");

    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, July 30, 2018 11:45 AM
  • Hi Sabah, 
    I've tried that encoding as well still no luck.


    Monday, July 30, 2018 11:47 AM
  • One issue could be this.. you are uploading file in GB2312 encoding but internally all data are handling in UTF encoding. See if you can figure out if this is the issue or not.

    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, July 30, 2018 12:09 PM
  • Sorry, bro, but this is your orginal code. It has no error handling, it mangles different aspects and concerns of message creation. And you haven't told us yet, where the exeption happens. Nor have you posted a stack trace.

    Programming and especially debugging is the art of isolating the problem. The first step is clean code. And to create a concise and complete example. 

    Monday, July 30, 2018 12:32 PM
  • The encoding is only necessary for the file name. Which works, just take a look at my example.

    Monday, July 30, 2018 12:33 PM
  • Exception as follows: 

    Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.

    Source Error: 

    Line 83:                 request.Source = from;
    Line 84:                 request.Destinations.Add(To);
    Line 85:                 var response = client.SendRawEmail(request); //error
    Line 86:                 lb2.Text = response.ToString();
    Line 87:                // reader.Dispose();

    Stack Trace: 

    [WebException: The remote server returned an error: (400) Bad Request.]
       System.Net.HttpWebRequest.GetResponse() +1399
       Amazon.Runtime.AmazonWebServiceClient.getResponseCallback(IAsyncResult result) +125

    [AmazonSimpleEmailServiceException: Expected parameter value, got "è"]
       Amazon.Runtime.AmazonWebServiceClient.endOperation(IAsyncResult result) +177
       Amazon.SimpleEmail.AmazonSimpleEmailServiceClient.SendRawEmail(SendRawEmailRequest request) +27
       _Default.bt1_c(Object sender, EventArgs e) in e:\Hanifa\Default.aspx.cs:85
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9815774
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

    • Edited by Hani2693 Tuesday, July 31, 2018 6:47 AM
    Tuesday, July 31, 2018 4:56 AM
  • [WebException: The remote server returned an error: (400) Bad Request.]
       System.Net.HttpWebRequest.GetResponse() +1399
       Amazon.Runtime.AmazonWebServiceClient.getResponseCallback(IAsyncResult result) +125

    Use fiddler and a working demo to see how a correct request should look like, then compare it with your request.

    When I need to guess: It's your weird to memory stream conversion.

    Tuesday, July 31, 2018 8:57 AM
  • Hi Stefan,

    Could you tell me some other way for the memory stream conversion?
    Tuesday, July 31, 2018 11:55 AM
  • No, cause I don't even know, why you need it at all..

    Tuesday, July 31, 2018 12:00 PM
  • The whole stream construction thing seems to be a rather dodgy way of getting the underlying data for the purposes of using this 'raw message' email using AmazonWebServiceClient.

    You may want to try just sending your MailMessage using the SmtpClient instead just to see if that works.

    If that works but the AmazonWebServiceClient doesn't then you may be better directing your question to the AWS Developer forums.

    • Edited by RJP1973 Tuesday, July 31, 2018 4:03 PM
    Tuesday, July 31, 2018 4:02 PM
  • Is it possible to write to stream directly without the conversion thing?
    Like when we use MIME we can write to the stream using this piece:


     var message = (MimeMessage)MyMailMessage();
            var stream = new MemoryStream();
      message.WriteTo(stream); // puts the message in the stream

    We later call that by :

     using (var client = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(credentials, RegionEndpoint.USEast1))
               var request = new SendRawEmailRequest { RawMessage = new RawMessage { Data = stream  } };
                var response = client.SendRawEmail(request);
                lb1.Text = response.ToString();

    Is that Possible for System.net?

    Wednesday, August 1, 2018 8:32 AM
  • Possible? Yes.

    Necessary? We don't know. Especially as you haven't posted anything about that external API/library.

    When I need to guess: Normally an mail requiring a stream, means that you need a text stream (StreamReader).

    Wednesday, August 1, 2018 9:31 AM
  • So how do i populate my message into the stream , because i tried using this code it sends me an empty mail with no body.

    string attachment1 = Server.MapPath(Path.GetFileName("跃科人才思想领导力活动调研.pdf"));
    using (var client = AWSClientFactory.CreateAmazonSimpleEmailServiceClient(credentials, RegionEndpoint.USEast1))
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
                ms.Position = 0;
                System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
                System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, ct);
                attach.ContentDisposition.FileName = "跃科人才思想领导力活动调研.pdf";
                   StreamReader reader = new StreamReader(ms, System.Text.Encoding.GetEncoding("ISO-8859-1"),true);
                    rawMessage.Data = ms;
                   SendRawEmailRequest request = new SendRawEmailRequest();
                    request.RawMessage = rawMessage;
                    request.Source = from;
                    var response = client.SendRawEmail(request);
                    lb2.Text = response.ToString();
    Libraries used
    using System.IO;
    using Amazon.Runtime;
    using Amazon;
    using Amazon.SimpleEmail.Model;
    using System.Net.Mail;

    • Edited by Hani2693 Wednesday, August 1, 2018 11:25 AM
    Wednesday, August 1, 2018 11:14 AM
  • Well, we can read for sure usings...

    But do you expect us to search for those libraries? They don't appear from nowhere on my machine..

    Wednesday, August 1, 2018 1:17 PM