Help

Copper Contributor

Can’t send emails

whats wrong with my code?

 

//MailSubject = fn("Mail.Subject"];

MailSMTP = fn("Mail. SMTP");

MailPort = fnNumber ("Mail.SMTP.Port");

public void Send(string subject, string body, string mailTo, Dictionary<string, byte[] › attachments)

var fromAddress = new MailAddress(MailOrigin, MailMask);

var toAddress = new MailAddress(mailTo, mailTo);

var smtp = new SmtpClient

EnableSsl = true,

UseDefaultCredentials = false,

Credentials = new NetworkCredential(fromAddress.Address, MailPassword, MailSMTP),

Host = MailSMTP,

Port = MailPort,

DeliveryMethod = SmtpDeliveryMethod.Network

};

using (var message = new MailMessage(fromAddress, toAddress)

Subject = subject,

Body = body

if (attachments != null && attachments.Count > 0)

attachments. ToList().ForEach(x →> {

message.Attachments.Add (new Attachment(new MemoryStream(x.Value), x.Key));

smtp. Send (message) ;

1 Reply

Hi @ahirschp785,

here are some errors that i have found in your code:

- The square brackets in the line "MailSubject = fn("Mail.Subject"];"" seem to be incorrectly placed. The corrected line should be "MailSubject = fn("Mail.Subject");".

- The characters "};" at the end of the SmtpClient initialization block appear to be incorrect syntax. The correct syntax should be "};".

- The lambda expression "x →>" is not valid. It should be replaced with "x =>" for proper syntax.

 

MailSubject = fn("Mail.Subject");
MailSMTP = fn("Mail.SMTP");
MailPort = fnNumber("Mail.SMTP.Port");

public void Send(string subject, string body, string mailTo, Dictionary<string, byte[]> attachments)
{
    var fromAddress = new MailAddress(MailOrigin, MailMask);
    var toAddress = new MailAddress(mailTo, mailTo);
    
    var smtp = new SmtpClient
    {
        EnableSsl = true,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, MailPassword),
        Host = MailSMTP,
        Port = MailPort,
        DeliveryMethod = SmtpDeliveryMethod.Network
    };

    using (var message = new MailMessage(fromAddress, toAddress)
    {
        Subject = subject,
        Body = body
    })
    {
        if (attachments != null && attachments.Count > 0)
        {
            attachments.ToList().ForEach(x =>
            {
                message.Attachments.Add(new Attachment(new MemoryStream(x.Value), x.Key));
            });
        }

        smtp.Send(message);
    }
}


Enter the inputs in the start of your script and try to run it.

Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.


If the post was useful in other ways, please consider giving it Like.


Kindest regards,


Leon Pavesic
(LinkedIn)
(Twitter)