Feb 24 2024 07:07 AM
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) ;
}
Feb 27 2024 07:06 AM
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.