I have just the sender name not the mail sender address which i want to set for the particular mail message and i just want to set just the name but when setting it using the following code throws me the exception :“The specified string is not in the form required for an e-mail address” and that is right but how can I set just the sender name?
Code:
MailMessage mail = new MailMessage();
mail.From = “David”;
@Anjali12
You want the PST entry to show only a sender name such as “David” without an e‑mail address.
Using MailMessage
MailMessage.From expects a valid e‑mail address, but you can supply a placeholder address and set the display name. The display name is written to the PST as the sender name [2].
using Aspose.Email;
using Aspose.Email.Mapi;
// create a MailMessage with a placeholder address and a display name
MailMessage mail = new MailMessage();
mail.From = new MailAddress("placeholder@domain.com", "David"); // address required, name used
mail.To.Add("recipient@example.com");
mail.Subject = "Test";
mail.Body = "Body text";
// convert to a MAPI message and add to a PST file
MapiMessage mapi = MapiMessage.FromMailMessage(mail);
PersonalStorage pst = PersonalStorage.Create("output.pst", FileFormatVersion.Unicode);
FolderInfo inbox = pst.RootFolder.GetSubFolder("Inbox");
inbox.AddMessage(mapi);
The placeholder address satisfies the format requirement, while the DisplayName (“David”) appears as the sender name in the PST.
Using MapiMessage directly
If you prefer not to include any address at all, create a MapiMessage and set its SenderName property, which does not require an e‑mail address [6].
using Aspose.Email.Mapi;
MapiMessage mapi = new MapiMessage();
mapi.SenderName = "David"; // only the name, no address
mapi.Subject = "Test";
mapi.Body = "Body text";
PersonalStorage pst = PersonalStorage.Create("output.pst", FileFormatVersion.Unicode);
FolderInfo inbox = pst.RootFolder.GetSubFolder("Inbox");
inbox.AddMessage(mapi);
SenderName maps to the MAPI property PR_SENDER_NAME, so the PST entry will show “David” as the sender without any e‑mail address.