最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest - Azure Communications Service api - add new from address - Stack Overflow

programmeradmin5浏览0评论

I am testing the REST API and my post request is working (an email is successfully sent). However, I want to change the from address to something other than the default. I believe there is no way to do this through the api but it should be possible through the Azure portal here:

Home > Communication Services > click my service > Domains > click my domain > MailFrom addresses

It shows one MailFrom address (DoNotReply@domain). I want to add a new MailFrom address, for example "MyApp" <myapp@domain> The problem is the 'Add' button is greyed out. See screenshot below.

How can I add a new MailFrom address?

My custom domain is connected successfully to ACS and it is verified for everything (SPF, DKIM etc).

Thank you

I am testing the REST API and my post request is working (an email is successfully sent). However, I want to change the from address to something other than the default. I believe there is no way to do this through the api but it should be possible through the Azure portal here:

Home > Communication Services > click my service > Domains > click my domain > MailFrom addresses

It shows one MailFrom address (DoNotReply@domain). I want to add a new MailFrom address, for example "MyApp" <myapp@domain> The problem is the 'Add' button is greyed out. See screenshot below.

How can I add a new MailFrom address?

My custom domain is connected successfully to ACS and it is verified for everything (SPF, DKIM etc).

Thank you

Share Improve this question edited 18 hours ago VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked yesterday ServerBlokeServerBloke 8523 gold badges16 silver badges28 bronze badges 1
  • do want to add custom doam withrest api – Sampath Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

From this Microsoft Documentation, you can add sender addresses to the Email Communication Service. If you encounter issues while adding an email, refer to this Microsoft Q&A.

Using Azure CLI:

You can use the following Azure CLI command:

az communication email domain sender-username create --email-service-name "<EmailServiceName>" --resource-group "<resourceGroup>" --domain-name "contoso" --sender-username "contosoNewsAlerts" --username "contosoNewsAlerts" --subscription "<subscriptionId>"

Below is a sample Python code to interact with the Azure Communication Services Email API using REST for custom domain:

import requests
import json
from azure.identity import DefaultAzureCredential

SUBSCRIPTION_ID = "SUBSCRIPTION_ID"
RESOURCE_GROUP = "RESOURCE_GROUP"
EMAIL_SERVICE_NAME = "EMAIL_SERVICE_NAME"
API_VERSION = "2023-04-01"

BASE_URL = f"https://management.azure/subscriptions/{SUBSCRIPTION_ID}/resourceGroups/{RESOURCE_GROUP}/providers/Microsoft.Communication/emailServices/{EMAIL_SERVICE_NAME}"

def get_access_token():
    credential = DefaultAzureCredential()
    token = credential.get_token("https://management.azure/.default")
    return token.token

def list_domains():
    url = f"{BASE_URL}/domains?api-version={API_VERSION}"
    headers = {
        "Authorization": f"Bearer {get_access_token()}",
        "Content-Type": "application/json"
    }
    
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        domains = response.json()
        print("Available Domains:", json.dumps(domains, indent=2))
    else:
        print(f"Failed to list domains. Error: {response.text}")

def create_domain(domain_name):
    url = f"{BASE_URL}/domains/{domain_name}?api-version={API_VERSION}"
    headers = {
        "Authorization": f"Bearer {get_access_token()}",
        "Content-Type": "application/json"
    }
    payload = {
        "location": "Global",
        "properties": {
            "domainManagement": "CustomerManaged"
        }
    }
    
    response = requests.put(url, headers=headers, json=payload)
    if response.status_code in [200, 201]:
        print(f"Domain {domain_name} created successfully.")
    else:
        print(f"Failed to create domain. Error: {response.text}")
def list_sender_usernames(domain_name):
    url = f"{BASE_URL}/domains/{domain_name}/senderUsernames?api-version={API_VERSION}"
    headers = {
        "Authorization": f"Bearer {get_access_token()}",
        "Content-Type": "application/json"
    }
    
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        senders = response.json()
        print("Sender Usernames:", json.dumps(senders, indent=2))
    else:
        print(f"Failed to list sender usernames. Error: {response.text}")

def create_sender_username(domain_name, sender_username):
    url = f"{BASE_URL}/domains/{domain_name}/senderUsernames/{sender_username}?api-version={API_VERSION}"
    headers = {
        "Authorization": f"Bearer {get_access_token()}",
        "Content-Type": "application/json"
    }
    payload = {
        "properties": {
            "username": sender_username
        }
    }
    
    response = requests.put(url, headers=headers, json=payload)
    if response.status_code in [200, 201]:
        print(f"Sender Username {sender_username} created successfully.")
    else:
        print(f"Failed to create sender username. Error: {response.text}")

if __name__ == "__main__":
    print("Listing available domains...")
    list_domains()

    new_domain = "mynewdomain"
    print(f"Creating domain: {new_domain}")
    create_domain(new_domain)

    print(f"Listing sender usernames for domain {new_domain}")
    list_sender_usernames(new_domain)

    new_sender = "newSender"
    print(f"Creating sender username: {new_sender}")
    create_sender_username(new_domain, new_sender)

Output:

发布评论

评论列表(0)

  1. 暂无评论