While sending mail using office 365 how can we set different from address. Basically the credentials I am giving are for office 365 account/username and from email address I am mentioning the different account. So this has to be same?
Below is my code.
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("[email protected]", "To Name"));
msg.From = new MailAddress("[email protected]", "From Name");
msg.Subject = "Azure Web App Email using smtp.office365.com";
msg.Body = "Test message using smtp.office365.com on Azure from a Web App";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "O365 PASS");
client.Port = 587;
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
But it is giving me below error,
Mailbox unavailable. The server response was: 5.7.60 SMTP; Client does not have permissions to send as this sender
Basically I want to set the different from address while sending the mail.
EDIT 1 : The from email address will be the logged in users email id. This will be from same domain, but can we give the send as permission for common mailbox considering this.
That error mean the credentials your trying to send with doesn't have the SendAS permission for the user/mailbox your trying to send as the Sender. To fix that you will either need to grant that through the Portal https://technet.microsoft.com/en-AU/library/jj919240%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396 or using PowerShell
Add-ADPermission -Identity helpdesksupport -User [email protected] -ExtendedRights "Send As"